Vb.net VB-更改子对象上的颜色';行不通

Vb.net VB-更改子对象上的颜色';行不通,vb.net,Vb.net,我已经为通过所有控件的每个循环创建了一个循环,并在找到它时更改其颜色。问题是,它不会更改标签的颜色: 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.

我已经为通过所有控件的每个循环创建了一个循环,并在找到它时更改其颜色。问题是,它不会更改标签的颜色:

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

问题是,如果控件是
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

您是否尝试调试代码以查看流?我猜如果控件是
TableLayoutPanel
,那么您并不是在为其children@Pikoh,感谢您的快速回答,我对VisualStudio非常陌生,因此调试工具对我来说是全新的。你能给我一个提示,我应该从哪里开始寻找这方面的信息吗?那么,你的第一步就是学习在VisualStudio中调试。这是一项基本技能,请参阅有关调试的文档,其工作方式与其他软件类似。我发现For-Each循环遍历表布局面板外部的所有标签并更改它们。在“表格布局”面板中,它从不“选择/查找”标签。看来你走对了方向。我需要找到一种方法来访问表格布局面板中的标签。我还注意到它从来没有登陆过
SetColorSettings(c)
EDIT:Original code:试试我答案中的代码,告诉我是否有用