Event for Click in any button (C# windows forms)
Asked Answered
B

4

7

I'm developing a program that has many buttons that should do a similar action when clicked, but with a small difference based on which button was clicked. The problem is that the only straightforward path is to code this for each button, which would be a very repetitive task. Is there a way to program simply one block that would get the click on any button and which button was clicked?

Beyond answered 23/9, 2012 at 17:3 Comment(6)
Why not just code a single method that is called from each button handler with a different parameter?Topsyturvydom
That would still be repetitive. I have upwards of 70 buttons, and I'd still have to individually put each button's parameter there... And if I ever changed the method to receive other parameters, I'd have to change it all again...Beyond
Then why don't you assign the same handler to the MouseClick event of all the buttons. The handler's MouseEventArgs parameter has the Button property which gives you the button that was pressed.Topsyturvydom
@Topsyturvydom but that property tells you what mouse button was pressed, not which control on the Form.Paddie
If you are using 70 separate controls, then something somewhere needs to point their OnClick events at a handler. If that is a problem the you need 1 control, with one on click event, with 70 separate areas that can be clicked on.Corney
@codesparkle: Crap, then the sender argument is what is needed.Topsyturvydom
P
15

Assign the same event handler to all buttons.

foreach (var button in Controls.OfType<Button>()) {
    button.Click += button_Click;
}

Or you can select the same event handler in the properties window switched to events (flash icon).


private static void button_Click(object sender, EventArgs eventArgs)
{
    switch (((Button)sender).Name)
    {
        // find a way to disambiguate.
    }
}

You can also add some useful information to the Tag property for the disambiguation. And last but not least, you can derive your own button from Button and add appropriate properties. They will even appear in the properties window.

Paddie answered 23/9, 2012 at 17:13 Comment(1)
By the way, instead of comparing the name, you could compare object references, as shown in some other answers.Paddie
B
7

Create a button click handler by double-clicking one of the buttons. But instead of doing the same with the other buttons, go to the properties window and switch to events view. Now select each one of the remaining buttons in turn and choose the just created click handler from the drop down list of the Click event of the other buttons in the properties Window. Now they all trigger the same method when they are clicked.

enter image description here

private void button1_Click(object sender, EventArgs e)
{
    var btn = (Button)sender;
    switch (btn.Name) {
        case "button1":
            ...
            break;
        case "button2":
            ...
            break;
        case "button3":
            ...
            break;
        default:
            break;
    }
}

Or you can define a value for the Tag property of the buttons in the properties window and use it directly without having to use a switch- or if-statement.

You can also test for specific buttons directly with sender == button1, but this does not work in a switch statement.


It might be easier to create your own button deriving from Button and to add the required properties. Once compiled, your button appears in the Toolbox and your properties can be set in the properties window.

public class MyButton : Button
{
    public int A { get; set; }
    public int B { get; set; }
}

Usage:

private void button1_Click(object sender, EventArgs e)
{
    var btn = (MyButton)sender;
    DoSomething(btn.A, btn.B);
}
Bekah answered 23/9, 2012 at 17:21 Comment(0)
K
3

Is there a way to program simply one block that would get the click on any button and which button was clicked?

I would just use the same click event and conditionally check the sender for which button was clicked

private void button1_Click(object sender, System.EventArgs e)
{
   if(sender == button1)
   {
      //do something different for button1
   }
   else if(sender == button2)
   {
      ....
   }
}

Or a switch statement..

Keeleykeelhaul answered 23/9, 2012 at 17:21 Comment(0)
D
1

Yes you can create just one Button Click Event Handler and hook it up with all the buttons using visual studio designer.

It is simple, just follow these steps:

1) Create btn_click event handler for any one button by double clicking on any button. 2) For all other buttons, right click on any button, click properties, go to events, on "Click" event, select btn_click from the drop-down list.

If you want different functionality in different buttons in same event handler, you can downcast the sender parameter to Button type and then use its Name property to differentiate among buttons.

Here's an example:

private void btn_Click(object sender, System.EventArgs e)
{
   Button b =(Button)sender;
   if(b.Name == "button1")
   {
      //some code
   }
   else if(b.Name == "button2")
   {
      ....
   }
}
Doings answered 23/9, 2012 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.