I have 6 buttons that I want to be attached to the same handler. How can I do this?
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.
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
}
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.
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.
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.
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.
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;
}
}
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
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";
}
}
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
© 2022 - 2024 — McMap. All rights reserved.