Click here to Skip to main content
16,012,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use the Controls Collection to change the background color of several labels on a panel (panel1). The following code actually works to do this. All of the labels have been tagged with the same tag (lblX).

VB
Option Infer Off ' need this to use collections to change controls

Private Sub ChangeColor
        Dim ctrl As Control
        For Each ctrl In Controls
            If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
                Dim nestedControl As Windows.Forms.Control
                For Each nestedControl In ctrl.Controls
                    If nestedControl.Tag = "lbl1Pos" Then
                        nestedControl.BackColor = Color.Yellow
                    End If
                Next
            End If
        Next
End Sub


But if I put the labels in a second panel (panel2) that is placed on top of panel1 the code does not change the background color of labels.
Any thoughts on what needs to be changed to access the nested controls that are nested in the second panel???

This was quite easy with the control arrays available in VB6!

Thanks for any help
Gary Vogel
Posted
Updated 29-Sep-10 0:58am
v2

VB
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ctrls As ControlCollection = Me.Controls
        Dim c As Collection = New Collection

        CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Label), 0)
        CtrlColor(c, Color.Black, Color.White)
        c.Clear()
        CtrlCollection(ctrls, c, GetType(System.Windows.Forms.Panel), 0)
        CtrlColor(c, Color.White, Color.Black)
    End Sub

    Sub CtrlCollection(ByRef ctrls As Control.ControlCollection, ByRef c As Collection, ByRef systype As System.Type, ByRef nLevel As Integer)

        Dim Control As Control

        For Each Control In ctrls
            If Control.GetType Is systype Then
                c.Add(New Object() {Control, nLevel})
            End If

            If Control.GetType Is GetType(System.Windows.Forms.Panel) Then
                CtrlCollection(Control.Controls, c, systype, nLevel + 1)
            End If
        Next
    End Sub

    Sub CtrlColor(ByRef c As Collection, ByRef Ca As Color, ByRef Cb As Color)

        Dim obj As Object()

        For Each obj In c
            With obj(0)
                If Math.Ceiling(obj(1) / 2) = obj(1) / 2 Then
                    .BackColor = Ca
                    .ForeColor = Cb
                Else
                    .BackColor = Cb
                    .ForeColor = Ca
                End If
            End With
        Next
    End Sub

End Class
 
Share this answer
 
v3
Comments
GaryV100 29-Sep-10 9:47am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Each panel has its own control collection you need to loop through that.

i.e.

for each ctr as control in panel.controls
 if typeof ctr is textbox then
  ..do your stuff here
 end if
next
 
Share this answer
 
Comments
GaryV100 29-Sep-10 9:47am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Try something like this. Doesn't matter how deep it is.

Public Sub SetCtlColor(ByRef u_CtlColl as controlcollection)
    on error resume next

    For Each ctl as control in u_CtlColl

        if TypeOf ctl Is label AndAlso ctl.Tag = "lbl1Pos" Then

            ctl.BackColor = Color.Yellow

        ElseIf TypeOf ctl Is panel Then

            SetCtlColor(ctl.Controls)

        End If

    Next

End Sub
 
Share this answer
 
Comments
GaryV100 29-Sep-10 9:47am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
WOW...the things I do not know and would have never figured out my own!!!
Thanks to all of you for the great help!!!
Gary Vogel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900