我正在使用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>
答案 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