Array of buttons

  • Thread starter Thread starter saulij
  • Start date Start date
S

saulij

I have an array of buttons. There is a common Click-method for these
buttons.
How can I find what button is clicked?

Sauli
 
I have an array of buttons. There is a common Click-method for these
buttons.
How can I find what button is clicked?

Sauli

In the event handler, first arg would be "object sender". you can
identify the sender through this argument. (I believe so)

-Cnu
 
In the event handler, first arg would be "object sender". you can
identify the sender through this argument. (I believe so)

I haven't tried this but it should work.

Private void Common_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.Name)
{
case "btnButton1":
Do something for button1;
break;
case "btnButton2":
Do something for button2;
break;
etc ...
}
}
 
I haven't tried this but it should work.

Private void Common_Click(Object sender, EventArgs e)
{
    Button btn = (Button)sender;
    switch (btn.Name)
    {
        case "btnButton1":
            Do something for button1;
            break;
        case "btnButton2":
            Do something for button2;
            break;
        etc ...
    }



}- Hide quoted text -

- Show quoted text -

nice to see some code here. The same approach I was suggesting.

Saulij, Please let us know if it solves your issue.

-Cnu
 
Also, you could set the Tag property for each button and use that to identify
them.
 
Duggi kirjoitti:
nice to see some code here. The same approach I was suggesting.

Saulij, Please let us know if it solves your issue.

-Cnu
This works fine, Thanks a lot

sauli
 
Back
Top