我有一个包含多个Textbox和Comboboxes的表单。我已将主动控件的背面颜色属性设置为skyblue。我想将所有文本框和组合框的背面颜色设置为白色,同时它们不活动。
答案 0 :(得分:1)
这样做有正确的方法和错误的方式。你问错了方法。正确的方法是从TextBox派生自己的类并覆盖OnEnter和OnLeave方法。重复ComboBox。
但是你提出了错误的方法并且你可能试图添加这个功能太晚了所以我们不得不通过在运行时找回控件来将其搞砸。在表单类中添加一个构造函数,使其看起来像:
Public Sub New()
InitializeComponent()
FindControls(Me.Controls)
End Sub
Private Sub FindControls(ctls As Control.ControlCollection)
For Each ctl As Control In ctls
Dim match As Boolean
If TypeOf ctl Is TextBoxBase Then match = True
If TypeOf ctl Is ComboBox Then
Dim combo = DirectCast(ctl, ComboBox)
If combo.DropDownStyle <> ComboBoxStyle.DropDownList Then match = True
End If
If match Then
AddHandler ctl.Enter, AddressOf ControlEnter
AddHandler ctl.Leave, AddressOf ControlLeave
End If
FindControls(ctl.Controls)
Next
End Sub
Private controlColor As Color
Private Sub ControlEnter(sender As Object, e As EventArgs)
Dim ctl = DirectCast(sender, Control)
controlColor = ctl.BackColor
ctl.BackColor = Color.AliceBlue
End Sub
Private Sub ControlLeave(sender As Object, e As EventArgs)
Dim ctl = DirectCast(sender, Control)
ctl.BackColor = controlColor
End Sub