How can I include the "for" loop in the name of the button?
for (int i = 1; i <= 10; i++)
{
button[i].Text = "0";
}
Is there any way to make this work? Thank you!
How can I include the "for" loop in the name of the button?
for (int i = 1; i <= 10; i++)
{
button[i].Text = "0";
}
Is there any way to make this work? Thank you!
Yes, you can access a button with it's name like this:
for (int i = 1; i <= 10; i++)
{
string buttonName = "button" + i;
this.Controls[buttonName].Text = "0";
}
Initialize an array of Button
:
Button btnsarr = new Button[ DefineTheSize ]();
and add all buttons
in it:
btnarr[0] = button1;
btnarr[1] = button2;
btnarr[2] = button3;
// and so on
now you can use this array in the you want.
for (int i = 1; i <= btnarr.Length; i++)
{
btnsarr[i].Text = "0";
}
Best way to do this is:
foreach (Button button in this.Controls.OfType<Button>().ToArray())
button.Text = "0";