Using Generic Event Handler for Controls in Windows Forms!
I have bunch of controls which get populated from Database when the form Loads. I am using Dictionary (control.Name as Key and Control.Value as Value) to store inital values. When user changes v开发者_StackOverflow中文版alues I am using other Dictionary to load current values and compare it with inital Dictionary. If the values are different I am running some kind of code to prompt user of changes. I think it more hackish and looking for better solution. Please advise.
Thanks
try this
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
精彩评论