I am dynamically adding a bunch of controls to a form. Each control calls the same method, and in that method I need to know the array index of the the control that performed the action.
CheckBox[] myCB = new CheckBox[100];
int i;
for (i = 0; i < 100; i++)
{
myCB[i] = new CheckBox();
myCB[i].Text = "Clicky!";
myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
tableLayoutPanel1.Controls.Add(myCB[i]);
}
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
label1.Text = sender.???array index property???.ToString();
}
So if I click myCB[42]
label1
will read "42" Of course, if there is an easier way to handle dynamic controls I'd appreciate pointers.