Wednesday, November 7, 2012

Loop over all controls in a form

Loop over all controls in a form

Button, Label, CheckBox, RadioButton, TextBox, GroupBox, Progressbar, MenuItem, ListView Column Header (yes and is not over) are all controls inside a form.

One day or another, you need to do a loop over all your form to do something. You could some reason look for a particular control in your form and try to enable it or try to get a value from it. Who knows?
If you are reading this, you might have some problem accessing them and maybe you want a quick solution. Well I don’t have a quick solution for you, I might have one at the end of this post. I just want to show you first how you could have some trouble accessing the control and slowly suggest a way to jump over it.
Alright, check the next image. I quickly made a form with a lot of sub-menu in a MenuStrip (MenuItem). I place all the classical stuff (File, New, Save, Save As, Print, Quit, Options, Language, Edit, Copy, Cut, Paste….

I place a of control a little bit everywhere like Buttons, checkboxes, Label… like a all dressed pizza (Canadian pizza : sauce, pepperoni ,mozzarella  chesses, green pepper). OK. Please notice I placed a listView and I placed some controls inside a GroupBox and controls in a TabControl. By the way, you could download the ProjectSample at the end of this post if you don’t want to do it yourself. Alright, enough of talking take a look at the image:
You might do a loop that run over all the control in the form1. See the sample code here:

Public Class Form1
    Private str_temp As String
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        str_temp = ""
        For Each ctr As Control In Me.Controls
            str_temp = str_temp & ctr.Name & vbCrLf
    End Sub
End Class

Inside the load function, you are asking to run over all the Control inside the form. Me.Controls is a collection of all the controls inside form1. The keyword Me acts like this for C# users.
While the loop runs over all the control, it stores the names in a string a display the result at the end. You will notice that there are missing controls. You might be upset (happens all the time hahaha!)
So you want all the control for THIS form? Yes you can! You might not like what I will tell you. You have to loop again inside each GroupBox and each TabControl. And is not over… you need to loop in each TabControl every TabPage for additional control.

But why? Well … each control you put might be inside a container. A container is a container… so???? Each time you put a control inside a GroupBox, that control is not in your form, that control is in the GroupBox. The GroupBox itself is in the Form1.

Sorry if you are getting confused.

I don’t want to make this post to big. So I’ll cut it.
Next post, I’ll will try to loop in each control over and over. That is a loop in a loop with a for in a for, and then an other for and a if with a for until while …..
So this is exactly what you might do, looping over all the controls.

The next article after that silly solution will be an improvement.
Well, is getting late. Please try to read my next post: a loop in a loop, in a loop, in a loop….

References :

My web site : Check Technologies
Download Sample Project :

No comments:

Post a Comment