如何更改表单中所有标签的ForeColor?

时间:2016-09-02 01:36:40

标签: vb.net winforms visual-studio-2010

我正在使用Visual Basic .NET中的WinForms写下一个简单的日程安排计划应用程序,我有这个“编辑首选项”,我希望用户能够更改所有标签的颜色。我试着使用这段代码:

Public Sub darkMode(ByVal darkModeOn As Boolean)
    If darkModeOn Then
        For Each c As Control In Me.Controls
            If c IsNot lblTitle Then
                If TypeOf c Is Label Then DirectCast(c, Label).ForeColor = Color.FromArgb(255, 255, 255)
            ElseIf TypeOf c Is Panel Then
                For Each pnlPanel As Panel In Me.Controls.OfType(Of Panel)()
                    pnlPanel.BackColor = Color.FromArgb(66, 66, 66)
                Next
            End If
        Next
End Sub

但是当我运行该代码时,它只会更改表单中直接子级的ForeColor的{​​{1}},并且更改不适用于内部的Label其他容器如Label s。有没有办法更改所有Panel的{​​{1}},即使是那些不是ForeColor的直接子项的人?{/ p>

1 个答案:

答案 0 :(得分:1)

可能会改变If ElseIf会帮助你。

For Each c As Control In Me.Controls
        If TypeOf c Is Panel Then
            For Each pnlPanel As Panel In Me.Controls.OfType(Of Panel)()
                pnlPanel.BackColor = Color.FromArgb(66, 66, 66)
            Next
        ElseIf c IsNot lblTitle Then
            If TypeOf c Is Label Then DirectCast(c, Label).ForeColor = Color.FromArgb(255, 255, 255)
        End If
    Next