Say I have 10 text boxes and I want to put the same text into each of them. I don’t want to write textBoxNum. Text = "hello!" ten times so I might write something like this:
for(int i=1; i<=10; i++)
{
textBox + i. Text = "hello!";
}
Obviously, it doesn’t work.
How can this be done with a for loop ?
You either need to load all of your textboxes into a list or array structure, and this will allow you to iterate over it.
Otherwise, you could inspect the
Controlsproperty of your form/container for items of theTextBoxtype. If the controls could be nested in deeper containers, you might need to recursively explore them (at this point, I would seriously consider an array approach, unless you have some ghastly number of textboxes to load). But as a starting point, you might have