Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using this code to loop through the label controls in my form and change the forecolor but it does not work. What's wrong?


VB
Public Sub set_forecolor()
       
       For Each c As Control In Me.Controls.OfType(Of Label)()
           c.ForeColor = Color.Gold
       Next
 End Sub
Posted

you may have labels inside some other controls, try below method

How to find all child controls from a starting control or form[^]
then you can get all the controls as below
VB
Dim oList As List(Of Control) = Me.FindAllChildren()
For Each c As Control In oList.OfType(Of Label)()
    c.ForeColor = Color.Gold
Next

add module to your project and place below code inside that module
VB
<System.Runtime.CompilerServices.Extension()>
Public Function FindAllChildren(ByRef StartingContainer As System.Windows.Forms.Form) As List(Of System.Windows.Forms.Control)
    Dim Children As New List(Of System.Windows.Forms.Control)

    Dim oControl As System.Windows.Forms.Control
    For Each oControl In StartingContainer.Controls
        Children.Add(oControl)
        If oControl.HasChildren Then
            Children.AddRange(oControl.FindAllChildren())
        End If
    Next

    Return Children
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function FindAllChildren(ByRef StartingContainer As System.Windows.Forms.Control) As List(Of System.Windows.Forms.Control)
    Dim Children As New List(Of System.Windows.Forms.Control)

    If StartingContainer.HasChildren = False Then
        Return Nothing
    Else
        Dim oControl As System.Windows.Forms.Control
        For Each oControl In StartingContainer.Controls
            Children.Add(oControl)
            If oControl.HasChildren Then
                Children.AddRange(oControl.FindAllChildren())
            End If
        Next
    End If

    Return Children
End Function
 
Share this answer
 
v2
You can use recursion to move through all the controls.

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) 
  Private Sub SetPropertyX(ByVal t As Type, ByVal control As Control)
      Debug.WriteLine(control.Name)
      For Each child As Control In control.Controls
          If child.HasChildren Then
              SetPropertyX(t, child)
          End If
          If child.GetType Is t Then
              'This is were you would set your color
              child.ForeColor = Color.Gold
          End If
      Next
  End Sub

Call the SUB with a TYPE and the top container control:
VB
Dim type As Type = CType((New Label).GetType, Type)
   SetPropertyX(type, Me)


Hope this help, I have a small winform project that demonstrates this code, let me know if you would like it.

regs,

ron O.
 
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