How to retrieve the click on an array of buttons

1

I'm doing a mine field so I have to add an array of buttons on a panel (to be the houses). I created the array of buttons and added each [x] [y] button to a panel. But I do not know how to retrieve the click on a given [x] [y] button inside my array. I just know how to generate the event for a button, I do not know how I can "generalize" to the others, so, for example, when the [2] [3] button inside my matrix is clicked I get a " [3] was clicked "or" the positions clicked were x = 2, y = 3 ". I know it sounds like an obvious question, but I started it today in C #. Thank you.

    
asked by anonymous 24.12.2014 / 07:26

1 answer

1

You can use a single event generated for all buttons and within that event you check who fired it, something like this:

private void botoes_Click(object sender, EventArgs e)
{
    Console.WriteLine(((Button)sender).Name); // imprime no Console o nome do botão clicado
}

In this case, the sender will reference the component that performed the Click event, in case you need any specific information, you can put it in the Tag property of each button and then access it accordingly ((Button)sender).Tag

    
24.12.2014 / 10:56