I am trying to assign button controls text dynamically by using a loop like the following:
int ButtonNumber = 1;
while (ButtonNumber < 10)
{
//WHAT TO DO HERE!?
ButtonNumber++;
}
I wanna avoid the following:
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
Ideally, don’t have
button1,button2etc as variables in the first place. Have a collection (e.g. aList<Button>):(EDIT: You’d need to populate this somewhere, of course…)
then to update the text later:
This doesn’t play terribly nicely with the designer, unfortunately.
You can fetch a control by ID, but personally I try not to do that. If you really want to: