How can I make a single event handler handle ALL Button.Click events?

In my program, I have 9 buttons, and I have 9 separate event handlers for each of them, despite the fact that the code in each event handler is the same. It is proving to be very tedious to change the code for all of them. Is it possible to make a single Button.Click event handler that handles the Button.Click events for all of my buttons?


ANSWERS:


You can modify the Handles clause on the VS generated event code so that it can process the same event for multiple controls. In most cases, someone might want to funnel most, but not all, button clicks to one procedure. To change the Handles clause:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button1.Click, 
                 Button3.Click, Button4.Click ...
    ' just add the extra clicks for the additional ones

    ' you will need to examine "Sender" to determine which was clicked

    ' your code here
End Sub

This can also be done dynamically, such as for controls which are created and added to a form in the Load event (or where ever):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler Button1.Click, AddressOf AllButtonsClick
    AddHandler Button2.Click, AddressOf AllButtonsClick
    AddHandler Button3.Click, AddressOf AllButtonsClick

End Sub

To literately wire all buttons to the same event, you can loop thru the controls collection (uses Linq):

For Each b As Button In XXXXX.Controls.OfType(Of Button)
     AddHandler b.Click, AddressOf MyClickHandler
Next

Where XXXXX might be Me or a panel, groupbox etc - wherever the buttons are.


Here's how you could do it dynamically at run time so you don't have to write AddHandler for all the buttons:

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each ctrl In Me.Controls
        If (TypeOf ctrl Is Button) Then _
           AddHandler DirectCast(ctrl, Button).Click, AddressOf HandleButtonClick
    Next
End Sub

Of course, if this is too broad a stroke, you could put only the buttons you want to wire up to this event inside a panel, for example, and just loop throw the controls in YourPanel.Controls instead of the form. Or you could set the .Tag property on each button to some value and only wire the handler when that value matches. Just a couple ways I can think of.


You can wire your click events to one specific one. how you do it depends on the IDE you are using. if you are using Visual Studio then:

select a button control and press F4. This will open properties window for this control. In the events section (usually a little lightning icon) you can assign various events, which are supported by this particular control. Here you can select any specific or create your own event.



 MORE:


 ? How can I make a single event handler handle ALL Button.Click events?
 ? How can I make a single event handler handle ALL Button.Click events?
 ? VB.NET control with the same event handlers
 ? How to handle events for dynamically generated controls?
 ? Visual Basic Handles and Multiple Variables with same name
 ? Parameterizing the Handler of a sub
 ? How to add current date and time on this button click on windows form
 ? How to get the name of a button that's clicked outside it's individual event procedure
 ? C# - Adding an Event Handler for all instances of a class
 ? Working with Event Handlers in .NET