user424134
user424134

Reputation: 532

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 values 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

Upvotes: 1

Views: 465

Answers (1)

Smith
Smith

Reputation: 5959

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

Upvotes: 2

Related Questions