Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection of TextBoxes

Status
Not open for further replies.

kurtmo5

Programmer
Jul 16, 2004
18
US
How do I create a collection of textboxes so I can loop through them?
 
The best thing to do would probably be to create an array list to hold the references:

Code:
System.Collections.ArrayList arlTextBoxes = new ArrayList();

arlTextBoxes.Add(TextBox1);
arlTextBoxes.Add(TextBox2);
arlTextBoxes.Add(TextBox3);
...

foreach (TextBox txt in arl) {
   //Your loop code here
}

You could use an array in the above code if you know the number of textboxes up front.

You could also use the control collection of your form but all your textboxes will have to be directly on the form and not in any container controls like panels or tabs. This also assumes that you want to loop through ALL of the textboxes on the form:
Code:
foreach (Control ctl in this.Controls) {
   if (ctl is TextBox) {
      TextBox txt = (TextBox) ctl;
      //Your loop code here
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top