我在表单上有一个“应用”按钮,该表单最初具有属性Enabled = False
,但是当更改来自任何GroupBox
的控件时,应设置Enabled = True
。< / p>
我在表单上有几个GroupBox控件,我希望我可以简单地检查其中任何一个控件是否已更改。但是,虽然您似乎可以挂钩ControlAdded
和ControlRemoved
事件,但没有ControlChanged
事件。
所以我现在正在设置如下(实际检查有超过5个控件)。
有没有更有效的方法来做到这一点,因为我可以看到这会导致将来出现问题(例如,如果选项被添加到组中)?感谢。
Private Sub ControlChanged(sender As Object,
e As EventArgs) Handles txtUsername.KeyUp,
chkRestoreIEFavourites.CheckedChanged,
chkRestoreNicknames.CheckedChanged,
chkRestoreDesktop.CheckedChanged,
chkRestoreQuickLaunch.CheckedChanged
Me.btnApply.Enabled = True
End Sub
答案 0 :(得分:0)
唉,我已经找到了这样做的方法......
下面现在添加处理程序(Shown
),以便更改表单CheckBox
内的任何TextBox
或GroupBox
,Apply
按钮设置为Enabled = True
。当from为Closing
时,我也会删除句柄。
这是否是最好的做法我不知道,但现在这是我所知道的唯一方式。欢迎评论!
Private Sub formSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetupChangeHandlers()
End Sub
Private Sub formSettings_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing
SetupChangeHandlers(Remove:=True)
End Sub
Private Sub SetupChangeHandlers(Optional ByVal Remove As Boolean = False)
For Each GroupBox As GroupBox In Me.sptSettings.Panel2.Controls.OfType(Of GroupBox)() _
.OrderBy(Function(b) b.TabIndex)
For Each Control As Control In GroupBox.Controls.OfType(Of Control)() _
.OrderBy(Function(b) b.TabIndex)
If TypeOf Control Is CheckBox Then
Dim Checkbox = CType(Control, CheckBox)
If Not Remove Then : AddHandler Checkbox.CheckedChanged, AddressOf GroupBoxControl_Changed
Else : RemoveHandler Checkbox.CheckedChanged, AddressOf GroupBoxControl_Changed
End If
ElseIf TypeOf Control Is TextBox Then
Dim TextBox = CType(Control, TextBox)
If Not Remove Then : AddHandler TextBox.TextChanged, AddressOf GroupBoxControl_Changed
Else : RemoveHandler TextBox.TextChanged, AddressOf GroupBoxControl_Changed
End If
ElseIf TypeOf Control Is MaskedTextBox Then
Dim MaskedTextBox = CType(Control, MaskedTextBox)
If Not Remove Then : AddHandler MaskedTextBox.TextChanged, AddressOf GroupBoxControl_Changed
Else : RemoveHandler MaskedTextBox.TextChanged, AddressOf GroupBoxControl_Changed
End If
End If
Next
Next
End Sub
请注意,您需要导入System.Linq
和System.Windows.Forms
才能使用此解决方案。