Thursday, November 8, 2012

Loop over controls in container

Loop over controls in container


Multiple loop could make your code impossible to debug.

Last post [Loop over all control in a form ], i show you that you can’t loop over all control in your form if you have control inside a container. A container could be a GroupBox , a TabControl or a Panel. You have controls inside Statusbar and MenuStrip and again, you can’t access them easily.

So what do we do? Here, I will do something natural but surely not efficient. I will create as much loop necessary to get all my control because I have no way to get all my control in my form directly.
I was painful to do the code and honestly, I haven’t done anything extraordinary.  I simply put the name of each control in a simple string to display them. Imagine how would it be if I wanted to do something more complex with more conditions.

Imagine 1 second if you wish to exclude some control from your search. Will you do a kind of if-not condition into each type of container? And how will you do your error handling? Oh my good!!!
Take a look at this code; surely you have done something like this in your life. Check how hard is to understand this:


Public Class Form1
    Private str_temp As String
    ''' <summary>
    ''' I think this sub will loop over all the control, but this is not the best way to do thing
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'oldloop()


        Dim ctrGB As GroupBox
        Dim ctrGB1 As GroupBox
        Dim ctrTC As TabControl
        Dim ctrMI As ToolStripItem
        Dim ctrMS As MenuStrip
        str_temp = ""
        For Each ctr As Control In Me.Controls

            If TypeOf ctr Is GroupBox Then
                ctrGB = CType(ctr, GroupBox)
                For Each ctr1 As Control In ctrGB.Controls
                    If TypeOf ctr1 Is GroupBox Then
                        ctrGB1 = CType(ctr1, GroupBox)
                        str_temp = str_temp & ctrGB1.Name & vbCrLf
                    ElseIf TypeOf ctr1 Is TabControl Then
                        ctrTC = CType(ctr1, TabControl)
                        For Each ctrTP As TabPage In ctrTC.TabPages
                            For Each ctrTP1 As Control In ctrTP.Controls
                                str_temp = str_temp & ctrTP1.Name & vbCrLf
                            Next
                            str_temp = str_temp & ctrTP.Name & vbCrLf
                        Next
                        str_temp = str_temp & ctrTC.Name & vbCrLf
                    Else
                        str_temp = str_temp & ctr1.Name & vbCrLf
                    End If
                Next
                str_temp = str_temp & ctrGB.Name & vbCrLf
            ElseIf TypeOf ctr Is TabControl Then
                ctrTC = CType(ctr, TabControl)
                For Each ctrTP As TabPage In ctrTC.TabPages
                    For Each ctrTP1 As Control In ctrTP.Controls
                        str_temp = str_temp & ctrTP1.Name & vbCrLf
                    Next
                Next
                str_temp = str_temp & ctr.Name & vbCrLf
            ElseIf TypeOf ctr Is MenuStrip Then

                ctrMS = CType(ctr, MenuStrip)
                str_temp = str_temp & ctrMS.Name & vbCrLf
                For index1 = 0 To ctrMS.Items.Count - 1 Step 1
                    Dim strTS As ToolStripMenuItem
                    strTS = ctrMS.Items(index1)
                    str_temp = str_temp & strTS.Name & vbCrLf
                    For index2 = 0 To strTS.DropDownItems.Count - 1 Step 1
                        ctrMI = strTS.DropDownItems.Item(index2)
                        str_temp = str_temp & ctrMI.Name & vbCrLf
                    Next
                Next
                    str_temp = str_temp & ctrMS.Name & vbCrLf
            ElseIf TypeOf ctr Is StatusStrip Then
                ' ok, i am now too lazy to do work here, you understand
                ' all the work
            Else
                    str_temp = str_temp & ctr.Name & vbCrLf
            End If
        Next
        MsgBox(str_temp)


    End Sub

    Private Sub oldloop() 'this loop will not reach every loop in the form
        str_temp = ""
        For Each ctr As Control In Me.Controls
            str_temp = str_temp & ctr.Name & vbCrLf
        Next
        MsgBox(str_temp)
    End Sub
End Class





I you run the program; of course you might have the entire control name from my sample program.
I haven’t suggested anything better in this post. I will leave at is. Fell free to comment here and bring suggestions.

I don’t thing this is specially a Visual Basic problem. It happens to everyone.
It happens to me when I have to do something quickly and don’t have the time to make a clean code.


References :


My web site : Check Technologies
Download Sample Project : SamplePrivatePublic.zip



No comments:

Post a Comment