-1

I program a game in c# and I created buttons array for the game board. The buttons defined in the code (I did not draw them on the panel). How can I creat an action that happens when the user clicks on the button?

achinoam
  • 13
  • 3

2 Answers2

0

You say you have an array of buttons:

Button[] buttons = new Button[10]; // Let's say 10
for (int i=0; i<buttons.Length; i++)
{
    buttons[i] = new Button();
    buttons[i].Click += button_Click;
}

private void button_Click(object sender, EventArgs e)
{
     // This method is invoked when any of the buttons is clicked
}
George Findulov
  • 769
  • 6
  • 16
0

You could add something like this:

Button _myButton= new Button();
button.Click += (s,e) => { your code; };
button.Click += new EventHandler(myButton_Click);
container.Controls.Add(button);
etrupja
  • 2,710
  • 6
  • 22
  • 37