更改多个控件(后退/前移)颜色vb.net [英] Changing multiple control's (back/fore)color vb.net

查看:157
本文介绍了更改多个控件(后退/前移)颜色vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



好​​吧,我有一个装有很多(Label / TextBox)的表单,我在表单中有一个颜色切换选项,和我正在使用这个简单的代码:

 TLab.ForeColor = My.Settings.B 
HLine1.BackColor = My.Settings.B
HLine2.BackColor = My.Settings.B
UpdateLog.ForeColor = My.Settings.B



*那只有4个控件,我有近90个控件可供更改。



正如你所看到的,我不仅仅是改变标签的BackColor,但我也正在改变一个不同标签或文本框的ForeColor。



我尝试过:



所以我在思考循环或简单的事情。如下所示:D:

  Dim  Obj = {TLab.ForeColor,HLine1.BackColor,HLine2。 BackColor,UpdateLog.ForeColor} 
对于 每个 NC Obj
NC = My.Settings.B
下一步





在此先感谢:)

解决方案

您可以枚举表单中的控件而无需保留单独的列表对象。



可能最好作为递归函数完成,因为你可以在容器控件中有容器控件,下面的例子只有两个深度。



 对于 每个 x 作为控制 form1.Controls 
如果 x.Has孩子然后
'
' 枚举容器控件
'
对于 每个 z 作为控制 x.Controls
如果 TypeOf (z) CheckBox 然后
' 使用复选框做你想做的事
ElseIf TypeOf (z) TextBox 然后
' 等。
ElseIf TypeOf (z) NumericUpDown 然后
' 等。
ElseIf TypeOf (z) ComboBox 然后
' 等。
结束 如果
else
'
' 不是容器控件,处理它
'
如果 TypeOf (z) CheckBox 然后
' 使用复选框执行所需操作
ElseIf TypeOf (z) TextBox 然后
' 等。
ElseIf TypeOf (z) NumericUpDown <跨度lass =code-keyword>然后
' 等。
ElseIf TypeOf (z) ComboBox 然后
' 等。
结束 如果
end 如果
下一步


迈克尔提出了一个非常好的解决方案。但是,如果所有控件都使用OfType(Of Type)参数属于某种类型,则可以缩短代码。标签不是容器控件,所以下面的递归函数是没有意义的,但你可以将它用于你想要的任何控件类型。

 ' 使用父控件作为参数将函数放入子函数中,以便例程可以用于其他对象。例如将Panel1作为MainParent传递,它只会影响Panel1内的控件 
公共 Sub ChangeLabelColors( ByRef MainParent as Control)

' 检查是否有任何要更改的控件
如果 MainParent.HasChildren 然后 退出 Sub

' 仅使用OfType枚举标签控件
对于 每个 x as 标签 MainParent.Controls.OfType( of 标签)
' 递归调用子容器控件的函数
如果 x.HasChildren 然后 ChangeLabelColors(x)

' < span class =code-comment>假设你使用Michael的标签建议指示不改变控件的颜色(非常好主意),这将跳过带有.Tag不要改变我的控件
如果 x.Tag = 不要改变我 然后
x.BackColor = My.Settings.MCB
x.ForeColor = My.Settings.MCF
结束 如果
下一步

结束 Sub





只需致电:

 ChangeLabelColors( Me 





另外,这可以修改为允许你过滤类型也是如此。这基本上是迈克尔的解决方案,现在允许递归。我也选择使用Select Case,我相信它会更快(不确定)



 公共  Sub  ChangeControlColors( ByRef  MainParent  as  Control)

如果 MainParent.HasChildren < span class =code-keyword>然后 退出 Sub

对于 每个 x as Control < span class =code-keyword> in MainParent.Controls()

选择 案例 True
案例 TypeOf x 标签, TypeOf x 复选框, TypeOf x Panel
如果 x.HasChildren 那么 ChangeControlColors(x)
如果 x.Tag = 不要改变我 然后
x.BackColor = My.Settings.MCB
x.ForeColor = My.Settings.MCF
End 如果
结束 选择
下一步

结束 Sub


Hello.

Well, I have a form loaded with lots of (Label/TextBox)s, and i have a color switch option in the form, And I'm using the simple code which is this:

TLab.ForeColor = My.Settings.B
HLine1.BackColor = My.Settings.B
HLine2.BackColor = My.Settings.B
UpdateLog.ForeColor = My.Settings.B


*That's only 4 Controls, I have nearly 90 controls to change.

As you can see I'm not only changing a label's BackColor, But I'm also changing the ForeColor of a diffident label or textbox.

What I have tried:

So i was thinking about a loop or something simple. like the following :D:

Dim Obj = {TLab.ForeColor, HLine1.BackColor, HLine2.BackColor, UpdateLog.ForeColor}
For Each NC In Obj
    NC = My.Settings.B
Next



Thanks in Advance :)

解决方案

You can enumerate the controls in a form without keeping a separate list of objects.

Probably best done as a recursive function as you can have container controls inside container controls and the example below only goes two deep.

For Each x As Control In form1.Controls
           If x.HasChildren Then
           '
           ' Enumerate a containers controls
           '
               For Each z As Control In x.Controls
                       If TypeOf (z) Is CheckBox Then
                           ' Do what you want with a checkbox
                       ElseIf TypeOf (z) Is TextBox Then
                           ' etc.
                       ElseIf TypeOf (z) Is NumericUpDown Then
                           'etc.
                       ElseIf TypeOf (z) Is ComboBox Then
                           ' etc.
                       End If
           else
           '
           ' Not a container control, handle it
           '
              If TypeOf (z) Is CheckBox Then
                     ' Do what you want with a checkbox
              ElseIf TypeOf (z) Is TextBox Then
                     ' etc.
              ElseIf TypeOf (z) Is NumericUpDown Then
                     'etc.
              ElseIf TypeOf (z) Is ComboBox Then
                     ' etc.
              End If
           end if
 Next


Michael presents a very good solution. However, you can shorten your code if all of the controls are of a certain type using the OfType(Of Type) parameter. Labels are not Container Controls so the recursive function below is pointless, but you can use it for any control type you want.

' Put function in a sub using the parent control as a parameter so that the routine may be used for other objects. e.g. Pass Panel1 as MainParent and it will only affect the controls inside Panel1
Public Sub ChangeLabelColors(ByRef MainParent as Control)

' Check if there are any controls to change
If Not MainParent.HasChildren Then Exit Sub

'Use OfType to only enumerate Label controls
For Each x as Label in MainParent.Controls.OfType(Of Label)
    ' Recursively call the function for child container controls
    If x.HasChildren Then ChangeLabelColors(x)
    
    ' Assuming you are using Michael's tag suggestion indication not to change that control's color (Very good idea), this will skip controls with the .Tag "Don't Change Me"
    If Not x.Tag = "Don't Change Me" Then 
        x.BackColor = My.Settings.MCB
        x.ForeColor = My.Settings.MCF
    End If
Next

End Sub



Simply call:

ChangeLabelColors(Me)



Alternatively, this can be modified to allow you to filter the type as well. This is basically Michael's solution that now allows recursion. I also used Select Case by choice, I believe it be faster (Not sure about that)

Public Sub ChangeControlColors(ByRef MainParent as Control)
 
If Not MainParent.HasChildren Then Exit Sub
 
For Each x as Control in MainParent.Controls()

    Select Case True
        Case TypeOf x Is Label, TypeOf x is Checkbox, TypeOf x is Panel
            If x.HasChildren Then ChangeControlColors(x) 
            If Not x.Tag = "Don't Change Me" Then
                x.BackColor = My.Settings.MCB
                x.ForeColor = My.Settings.MCF
            End If
    End Select
Next
 
End Sub


这篇关于更改多个控件(后退/前移)颜色vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆