How can I subscribe multiple buttons to the same event handler and act according to what button was clicked?
Asked Answered
P

9

30

I have 6 buttons that I want to be attached to the same handler. How can I do this?

Psychodrama answered 28/9, 2010 at 15:13 Comment(2)
That's what I'm trying to avoid. I can double click the buttons on the designer view and generate 6 stubs, but why would I want to do that when I can just subscribe the buttons to a single event.Psychodrama
rather than double-click, drop the list down and select the existing handler...Vallombrosa
D
26

When you subscribe to the event on a button, it's just a standard event handler:

button1.Click += myEventHandler;

You can use the same code to add handlers for every button:

button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;

This will cause your handler in myEventHandler to be run when any of the buttons are clicked.

Deannadeanne answered 28/9, 2010 at 15:16 Comment(0)
D
41

You can attach the same event to multiple buttons by binding the same method to each buttons click event

myButton1.Click += new MyButtonClick;
myButton2.Click += new MyButtonClick;
myButton3.Click += new MyButtonClick;
myButton4.Click += new MyButtonClick;
myButton5.Click += new MyButtonClick;
myButton6.Click += new MyButtonClick;

void MyButtonClick(object sender, EventArgs e)
{
    Button button = sender as Button;
    //here you can check which button was clicked by the sender
}
Diplosis answered 28/9, 2010 at 15:15 Comment(2)
I have used CommandName and CommandArgument to identify the different buttons linked to same onclick event. I gave +1 for u.Hus
I had to remove the new keyword to make this work.Bechuanaland
D
26

When you subscribe to the event on a button, it's just a standard event handler:

button1.Click += myEventHandler;

You can use the same code to add handlers for every button:

button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;

This will cause your handler in myEventHandler to be run when any of the buttons are clicked.

Deannadeanne answered 28/9, 2010 at 15:16 Comment(0)
S
14

Just wire the buttons to the same event:

myButton1.Click += Button_Click;
myButton2.Click += Button_Click;
myButton3.Click += Button_Click;
...

And handle the buttons accordingly:

private void Button_Click(object sender, EventArgs e)
{
    string buttonText = ((Button)sender).Text;

    switch (buttonText)
    {
        ...
    }
}

The sender object contains the reference to the button which caused the Click event. You can cast it back to Button, and access whatever property you need to distinguish the actual button.

Spinelli answered 28/9, 2010 at 15:18 Comment(1)
The cast ended up with missing assembly information.Hillel
H
5

How to see which button was pressed:

Use the sender

Button myButton = (Button)sender;

sender is a parameter of type object in your event handler.

Haw answered 28/9, 2010 at 15:16 Comment(0)
F
4

Instead of double clicking the event in the designer you can paste the name of the event handler to to the event in the designer property grid.

Frowst answered 28/9, 2010 at 15:17 Comment(0)
M
2
myButton1.Click += new EventHandler(MyButtonClick);
myButton2.Click += new EventHandler(MyButtonClick);
myButton3.Click += new EventHandler(MyButtonClick);
myButton4.Click += new EventHandler(MyButtonClick);
myButton5.Click += new EventHandler(MyButtonClick);
myButton6.Click += new EventHandler(MyButtonClick);

public void MyButtonClick(object sender, MouseButtonEventArgs e)
{
                switch ((sender as Button).Name)
                {
                case "button1":
                    //actions
                    break;
                case "button2":
                    //actions
                    break;
                default:
                    break;
               }
}
Maffei answered 28/9, 2010 at 15:19 Comment(1)
It says "The type or name space for button could not be found."Hillel
E
1

I'm not an expert but I use this and it looks pretty simple

<Button x:Name="Button1" Content="Button" Click="Button_Click"/>
<Button x:Name="Button2" Content="Button" Click="Button_Click"/>

Just give both buttons the same click event

Elephant answered 9/12, 2016 at 14:23 Comment(0)
B
0

 button1.Click += button_Click;
 button2.Click += button_Click;

void button_Click(object sender, EventArgs e)
        {
            if (sender == button1)
            {
                this.Text = "1";
            }
            else if (sender == button2)
            {
                this.Text = "2";
            }
        }
Burchell answered 7/2, 2021 at 3:51 Comment(1)
Can you explain your answer a bit?Intermixture
A
-1

In main.axml add this to all the buttons

android:clickable="whateveryoulike"

and all the buttons need to have the same id

in the MainActivity

[Java.Interop.Export("whateveryoulike")]
void whateveryoulike2(View v){
Button button=(Button) v;
//your code here
}

the name of the method doesnt matter

Adrienadriena answered 29/11, 2017 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.