I have a form with 8 command button, I use AddHandler
for Click event for all buttons.
but when I press a button, cmbColor_Click
run twice.
Public Sub OpenForm()
AddHandler cmbColor1.Click, AddressOf cmbColor_Click
AddHandler cmbColor2.Click, AddressOf cmbColor_Click
AddHandler cmbColor3.Click, AddressOf cmbColor_Click
AddHandler cmbColor4.Click, AddressOf cmbColor_Click
AddHandler cmbColor5.Click, AddressOf cmbColor_Click
AddHandler cmbColor6.Click, AddressOf cmbColor_Click
AddHandler cmbColor7.Click, AddressOf cmbColor_Click
AddHandler cmbColor8.Click, AddressOf cmbColor_Click
End Sub
Private Sub cmbColor_Click(sender As Object, e As EventArgs)
Dim _color As New ColorDialog
Dim _button As Button = CType(sender, Button)
_color.Color = _button.BackColor
If _color.ShowDialog() = Windows.Forms.DialogResult.OK Then
_button.BackColor = _color.Color
End If
End Sub
If the event handler is being called twice for one event then the event handler has been attached to the event twice.
Ideally, you would track down why AddHandler is being used more than once and eliminate the problem.
However, as a quick fix, you can remove the event handler before adding it - it is not an error to try to remove a non-existent event handler.
So...