我创建了一个For Each循环,它遍历所有控件并在找到它时更改它的颜色。问题是,它不会改变标签的颜色:
Sub SetColorSettings(ByVal parent As Control)
parent.SuspendLayout()
For Each c As Control In parent.Controls
If TypeOf (c) Is TableLayoutPanel Then
c.BackColor = Color.White
ElseIf TypeOf (c) Is Label Then
c.ForeColor = Color.Black
Else
If c.HasChildren Then
SetColorSettings(c)
End If
End If
Next
parent.ResumeLayout()
parent.Refresh()
End Sub
然后我将SetColorSettings(Me)
的更改应用于另一个子或函数。
注意:在我的表单中,标签直接放在表格布局面板中,因此从技术上讲,标签应该是表布局面板的子项。
我个人认为这些内容正在弄乱我:
If c.HasChildren Then
SetColorSettings(c)
End If
答案 0 :(得分:1)
问题是,如果控件是TableLayoutPanel
,则表示您没有为其子项调用该方法。每次控件生孩子时都必须调用SetColorSettings(c)
。试试这段代码:
Sub SetColorSettings(ByVal parent As Control)
parent.SuspendLayout()
For Each c As Control In parent.Controls
If TypeOf (c) Is TableLayoutPanel Then
c.BackColor = Color.White
ElseIf TypeOf (c) Is Label Then
c.ForeColor = Color.Black
End If
If c.HasChildren Then
SetColorSettings(c)
End If
Next
parent.ResumeLayout()
parent.Refresh()
End Sub