Hi when I create textboxes on Windows Application Form I cannot name it as box[0], box[1] and so on. The purpose why I want to do like this is because I want to use them in a loop.
7 Answers
How about making a list of them after you create them? In your form initialization function, you can do something like:
List<TextBox> myTextboxList = new List<TextBox>();
myTextBoxList.Add(TextBox1);
myTextBoxList.Add(TextBox2);
mytextBoxList.Add(TextBox3);
Now you can itterate through with your "myTextboxList" with something like below:
Foreach (TextBox singleItem in myTextboxList) {
// Do something to your textboxes here, for example:
singleItem.Text = "Type in Entry Here";
}

- 253
- 1
- 8
-
Yes, List
would probably be better. You can add/remove textboxes easier. – Mert Akcakaya May 27 '12 at 01:52 -
where is that function? public MainForm() { InitializeComponent(); } this one? If so giving out an error... – Sarp Kaya May 27 '12 at 02:23
-
By the way how do I run this? does not work like myTextBoxList[0] – Sarp Kaya May 27 '12 at 02:26
-
@SarpKaya put it after the InitializeComponent(); – Cole Tobin May 27 '12 at 02:48
-
Your form initialization point will depend on what type of application you are making... It is windows, Gtk, Mac, etc.... Typically there will be an entry point to where you code starts, and you can include this there... If you aren't using an IDE, and are manually creating all your textboxes, you can simply use this code directly after you make your textboxes. – Jeff Halverson May 27 '12 at 06:16
-
I've updated my entry to show how you will then iterate through these. Because I used Generic List function and not an Array, you will access like I wrote, not with myTextBoxList[0] type of code. – Jeff Halverson May 27 '12 at 06:20
Actually I found TextBox[] array = { firstTextBox, secondTextBox };
works too!

- 3,686
- 21
- 64
- 103
You can create textboxes on runtime and just put them in an array...
If you want to do it in design time, you will have to do some control filtering logic on the whole this.Controls
array in order to access only the wanted textboxes. Consider if (currControl is TextBox)
if all textboxes in the form are ones you want in the array.
Another option for design time, is putting all wanted textboxes in a panel which will be their parent, and then iterating over the panel's children (controls) and cast them to TextBox.
A runtime solution would be something like:
var arr = new TextBox[10];
for (var i = 0; i < 10; i++)
{
var tbox = new TextBox();
// tbox.Text = i.ToString();
// Other properties sets for tbox
this.Controls.Add(tbox);
arr[i] = tbox;
}

- 14,044
- 4
- 38
- 60
I wouldn't use an array for this, personally. I would use some form of generic collection, like List.
List<TextBox> textBoxList = new List<TextBox>();
//Example insert method
public void InsertTextBox(TextBox tb)
{
textBoxList.Add(tb);
}
//Example contains method
public bool CheckIfTextBoxExists(TextBox tb)
{
if (textBoxList.Contains(tb))
return true;
else
return false;
}
You don't necessarily have to use the Contains method, you could also use Any(), or maybe even find another way- all depends on what you're doing. I just think using a generic collection gives you more flexibility than a simple array in this case.

- 1,949
- 4
- 25
- 45
-
Wow, it is weird to see function names starting with low character. It looks like Java .) – Mert Akcakaya May 27 '12 at 01:56
-
// Declaring array of TextBox
private System.Windows.Forms.TextBox[] txtArray;
private void AddControls(int cNumber)
{
// assign number of controls
txtArray = new System.Windows.Forms.TextBox[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
txtArray[i] = new System.Windows.Forms.TextBox();
}
}

- 14,455
- 21
- 138
- 171
TextBox[] t = new TextBox[10];
for(int i=0;i<required;i++)
{
t[i]=new TextBox();
this.Controls.Add(t[]);
}

- 15,374
- 13
- 103
- 121

- 2,387
- 3
- 22
- 38
-
Visual Studio 2013 flags "this.Controls.Add(t[]);" as a "Syntax error; value expected" – ftexperts Jul 19 '15 at 05:11