Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is there any way to handle all click event in 1 sub? i have 20 buttons.
Posted

Yes. It should be something like:
C#
foreach(Button button in allButtons) {
    button.Click += (sender, eventArgs) {
        ButtonClickHandler((Button)sender); // pay attention for type cast:
                                            // sender is guaranteed to have a runtime type Button
    };
}

void ButtonClickHandler(Button button) {
    // do something
}


But now, the problem is: OK, you use one handler for several buttons in this or some other ways. But do you really need to do exactly the same thing in response to a click of any of those buttons? Hardly. I would rather assume you want to do something different.

Here is what you would be tempted to do, but should not:
C#
void ButtonClickHandler(Button button) {
    if (button == myFirstButton) // ugly!
        DoOneThing(); // ugly!
    else if (button == mySeconfButton) // ugly!
        DoSomethineElse(); // ugly!
    // ... even uglier!
}


Can you see that it would completely defeat the purpose of one unified handler method? It could be much worse than adding its individual handler to each button. One common handler can only be useful when the handling it totally identical for all button, only data is different. But how to pass this data? In most cases, it would be enough to pass just the index of the button in the array, or something like that. It depends on the purpose of the buttons.

Something like this:
C#
for(int index = 0; index < allButtons.Length; ++index) {
    allButtons[index].Click += (sender, eventArgs) {
        ButtonClickHandler(index); // here, is a non-trivial feature is used: the closure
    };
}

void ButtonClickHandler(int index) {
    // do something using index as a parameter to some data array/collection
}


Are you getting the idea?

To finish with the critical review of the idea of using the common handler, I need to note: the whole idea of using 20 buttons is questionable, and this is a root of the problem. Why? You could think about a list box and one button; the effect of button click would depends on the selection in the list box. The benefit of this would be this: in a list box, you can store any data, not just the string; the list box will display whatever is returned by GetString of the element type.

I mentioned the closure in the code sample. It's very important to understand:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

—SA
 
Share this answer
 
Yes.
When you enter an event handler, you are given two parameters: sender which is an object, and e which is derived from EventArgs.
The sender parameter is the control which caused the event to occur. So if you have 20 buttons, and route all the Click events to the same handler, the sender will be the button that was clicked. Just cast it to a button, and you can use it to process the event:
VB
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, ...
    Dim b As Button = TryCast(sender, Button)
    If b IsNot Nothing Then
        Console.WriteLine(b.Text)
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900