When creating a new button, create a Click event with it:
public void CreateButtons(int NumX, int NumY)
{
this.Location = new Point(2, 2);
this.Size = new Size((NumX + 1) * 40 - 20, (NumY + 1) * 40);
int temp;
for (int i = 0; i < NumX; i++)
{
for (int j = 0; j < NumY; j++)
{
Buttons[i, j] = new Button();
Buttons[i, j].Name = "ItemNum_" + i.ToString();
Buttons[i, j].Location = new Point(40 * i, 40 * j);
Buttons[i, j].Size = new Size(40, 40);
temp = (r.Next(4) + 1);
Buttons[i, j].ImageIndex = temp;
label5.Text += " " + temp.ToString();
ColorTheButtons(Buttons[i, j]);
Buttons[i, j].Visible = true;
//CREATE A COMMON EVENT:
Buttons[i, j].Click += new EventHandler(Form1_Click);
this.Controls.Add(Buttons[i, j]);
//= Buttons .Controls.Find(Buttons[i,j].Name);
}
label5.Text += "\n";
}
}
private void Form1_Click(object sender, EventArgs e)
{
Button _button = sender as Button;
//NOW YOU CAN CONTINUE WITH THE CODE...
//_button HAS ALL THE PROPERTIES, ANS STUFF OF THE CLICKED BUTTON!
//FOR EXAMPLE:
string name = _button.Text;
}
And then you will have a common event handler for all the buttons.
Hope it helps,
Mitja
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474