在Windows窗体中使用通用事件处理程序控件!

时间:2011-07-01 02:15:58

标签: c# windows winforms events

当表单加载时,我有一堆控件从数据库中填充。我使用Dictionary(control.Name作为Key,Control.Value作为Value)来存储初始值。当用户更改值时,我使用其他词典加载当前值并将其与inital Dictionary进行比较。如果值不同,我正在运行某种代码来提示用户更改。我认为它更加hackish并寻求更好的解决方案。请指教。

由于

1 个答案:

答案 0 :(得分:2)

试试这个

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