How to loop through all controls on a form
There are times when a developer needs to loop through all of the controls on the form. Initially, a recursive method comes to mind, however, with VB.NET, it’s really a lot easier than you think.
In a regular Windows Forms Application, where “Me” represents the form, use the following snippet of code to loop through all controls on the form, even controls nested in various layers of panels, group boxes, etc.:
If you are looking for a control of a particular type, then just check for that type, and then perform your desired actions:
And that’s all there is to it!