Ideas for looping thru controls

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
I am familer with looping thru controls like so:

VB.NET:
Expand Collapse Copy
Public ChipColor(5) As String
Dim y As Integer
For Each Control As Control In Panel2.Controls
            If TypeOf Control Is ListBox Then
                Dim myList As ListBox = DirectCast(Control, ListBox)
                ChipColor(y) = myList.Text
                y = y + 1
            End If
Next

I have some labels on another form, that due to the design can't be in seperate panels. They are (lblChipColor1 - 6) and (lblChipValue1 - 7). I am trying to populate the color ones from the Array above.

I can obviously loop thru the array, but I can't find a decent way to loop thru just the lblChipColorx lables ignoring the lblChipValue1x labels. It would be easy if they were on seperate panels. I tried adding the labels names to an array of type string and of type label, but got nowhere with that.

Is there another way to group them, or build const array of label names, that I can work with?
 
Last edited:
I would add to your IF statement and compare part of the Label name to see if it matches your criteria.
 
Use the Tag property to identify those controls you want to include. Example:

VB.NET:
Expand Collapse Copy
        For Each ctl As Control In Controls
            If TypeOf ctl Is Label Then
                Dim lbl As Label = CType(ctl, Label)
                If lbl.Tag Is "OK" Then
                    lbl.Text = "New Text"  'or whatever
                 End If
            End If
        Next
 
Back
Top