First of all, most likely you will need to access the text boxes later. How? Simply make the reference
TextBox[] t
accessible during the life time you need. Most usually, you would need to make this array reference a field or a property of your form class. (Only give it a good semantic name; using one-character identifiers is always bad.)
You code has a number of problems to fix. Bad things is all your
immediate constants hard-coded in the code: 70, 50, 1142, 65, 100, 50, "txtbx"… Declare then all explicit constants or put in resources. Use a separate panel with docking/padding for a parent of these controls. One apparent problem is the size. You should never assign the height; it should remain default. One apparent problem that you arrange the controls by index without knowing its size. You should do a very different thing; better arrange them vertically and calculate the height to determine the vertical step:
int gap = 4;
for (int i = 0; i < count; i++) {
t[i] = new TextBox();
{
t[i].Location = new System.Drawing.Point(y * (t[i].Height + gap), x);
this.Controls.Add(t[i]);
}
}
Finally, remove
Refresh
and
ResumeLayout
. Sometimes you need
ResumeLayout
, but certainly out of the loop.
—SA