In addition to Solution 1, which needs to be improved:
Simpler, you can have a set of text boxes or other controls collected in a regular array:
TextBox[] textBoxes = new TextBox[] { txtBxFirstName, txtBxLastName, txtBxDOB, };
foreach(TextBox texBox in textBoxes) { }
You need to create a
textBoxes
object only if you are going to use it elsewhere, in about loop or LINQ, otherwise you would create the array right in the loop. The set is variable during the development, but is constant during run-time, so using a collection would be redundant.
But this would only look fine if you have not too many controls in a list/collection. If there are too many, it will became unmanageable, and the root cause would be: why would you create all those controls in a designer at all? So many mistakenly think that the designer is a kind of automation of development, but in fact it can be just a lot of manual work, bad for maintenance. If you have a big stack of buttons, putting them in a designer, taking care of alignment and other things would be just silly. Writing XAML by hand would be faster (copy and past ;-)), but not more reasonable. Main thing is: you don't really need individual names for so many buttons, you rather need to address then all via an array object like
textBoxes
. You rather need to create then in a loop in first place. Something like:
Grid parent = someGrid;
TextBox[] textBoxes = new TextBox[count];
for (int index; index < count; ++index) {
textBoxes[index] = new TextBox();
parent.Children.Add(textBoxes[index]);
parent.SetRow(textBoxes[index], index);
}
—SA