Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define an array of textboxes in c#?

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.

like image 283
Sarp Kaya Avatar asked May 27 '12 01:05

Sarp Kaya


2 Answers

Actually I found TextBox[] array = { firstTextBox, secondTextBox }; works too!

like image 141
Sarp Kaya Avatar answered Sep 17 '22 20:09

Sarp Kaya


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";
}
like image 40
Jeff Halverson Avatar answered Sep 19 '22 20:09

Jeff Halverson