我有一堆控件,当表单加载时,这些控件会从数据库中填充。我正在使用字典(control.Name 作为键,Control.Value 作为值)来存储初始值。当用户更改值时,我正在使用其他词典加载当前值并将其与初始词典进行比较。如果值不同,我将运行某种代码来提示用户进行更改。我认为它更骇人听闻,正在寻找更好的解决方案。请指教。
谢谢
最佳答案
试试这个
For Each ctrl As Control In me.Controls
If TypeOf ctrl Is CheckBox Then
AddHandler (DirectCast(ctrl, CheckBox).CheckedChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is TextBox Then
AddHandler (ctrl.TextChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is NumericUpDown Then
AddHandler (DirectCast(ctrl, NumericUpDown).ValueChanged), AddressOf Control_Changed
End If
Next
Sub Control_Changed(ByVal sender As Object, ByVal e As EventArgs)
' handle events here
End Sub
关于c# - 在 Windows 窗体中为控件使用通用事件处理程序!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542962/