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";
}
Controls collection like Selman22 mentioned in his answer already.Best way to do this is:
foreach (Button button in this.Controls.OfType<Button>().ToArray())
button.Text = "0";
this.Controls, you can add the buttons you want to treat with the loop into a list or an array, as shown in Shaharyar's answer, and then iterate over that.
0000000000?