Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 496
Kiloku Avatar asked Sep 23 '12 17:09

Kiloku


People also ask

How do you call a button Click event?

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method? private void SubGraphButton_Click(object sender, RoutedEventArgs args) { } private void ChildNode_Click(object sender, RoutedEventArgs args) { // call SubGraphButton-Click(). }

How do you call a button Click event in code behind?

To simulate the button click in code, you simply call the event handler: Button1_Click(object sender, EventArgs e). protected void Page_Load(object sender, EventArgs e) { //This simulates the button click from within your code. Button1_Click(Button1, EventArgs.

What is Click event C#?

Occurs when the Button control is clicked. public: event EventHandler ^ Click; public: virtual event EventHandler ^ Click; C# Copy.


4 Answers

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.

like image 171
Adam Avatar answered Sep 22 '22 13:09

Adam


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);
}
like image 45
Olivier Jacot-Descombes Avatar answered Sep 23 '22 13:09

Olivier Jacot-Descombes


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

like image 37
Jason Eades Avatar answered Sep 21 '22 13:09

Jason Eades


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")
   {
      ....
   }
}
like image 20
Amit Mittal Avatar answered Sep 23 '22 13:09

Amit Mittal