Question Looping through Labels does not complete

pgm575

Member
Joined
Jul 16, 2010
Messages
12
Programming Experience
Beginner
I have a strange issue, and I believe this is the right forum for it.

I have a form which contains a TabControl with 2 pages, which contains multiple GroupBoxes, which each contain a single TableLayoutPanel, which then each contain multiple Labels, Buttons, and Textboxes. I am using code which searches down to the bottom level in order to find said Labels, Buttons, and Textboxes, and then performs some action on them.

When use this code to perform operations on Buttons and Textboxes, it seems to work fine. When I use this code to do something with Labels, at some point the loop seems to just exit for no reason. The code which does this is shown below-

VB.NET:
Dim pg As System.Windows.Forms.TabPage, ctrl As System.Windows.Forms.Control, cctrl As System.Windows.Forms.Control,
            ccctrl As System.Windows.Forms.Control

        For Each pg In TabControl1.TabPages
            For Each ctrl In Me.TabControl1.TabPages(pg.Name.ToString).Controls
                If (TypeOf ctrl Is System.Windows.Forms.GroupBox) Then
                    For Each cctrl In ctrl.Controls
                        If (TypeOf cctrl Is System.Windows.Forms.TableLayoutPanel) Then
                            For Each ccctrl In cctrl.Controls
                                If (TypeOf ccctrl Is System.Windows.Forms.Label) Then
                                    'Perform some action
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        Next

What happens is this- after getting to the 5th or 6th Groupbox (and subsequent TableLayoutPanel), I can see that the selection of Labels has been properly identified as shown in here-
CodePic.jpg

But after it performs the operation for the first Label (Text= "ODxWT"), the loop simply exits. There is no Build error, no Runtime error, no notification whatsoever. Anyone seen this before?
 
mm im not sure, but have you tried putting that block of code inside of a try catch block to catch the exception? it happens to me alot where the code just exits when there is an error, be sure to catch it and then see what that error is.
 
Actually, I had already resolved this, but I hadn't been able to post a reply yet. As it turns out, during those Substring() tests, one of the inputs was too short to have a Substring of length 8; during runtime, the Sub simply exits at this point rather than throwing any kind of error, therefore not giving an indication of any improper inputs.
 
O cool, thanks for sharing. I think if you put that in a try catch block you should be able to catch the exception since this should be an out of range exception i think for an array?
 
I'll try this and see if it throws an exception where there might not have been one otherwise, as I now have another problem with a DataBinding that works when called one way and not another...
 
Back
Top