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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cycling through all text boxes

Status
Not open for further replies.

sjf905

Programmer
Sep 20, 2006
13
US
I've been programming for a while but am new to C#. I have a program with 16 text boxes and am hoping I can get all their results through a loop (or something similarly efficient) rather than have 16 lines of:

ResultArray[0] = TextBox1.Text;
ResultArray[1] = TextBox2.Text;
.
.
.
etc

The obvious probelm is of course the fact that the TextBox numbers are changing and hence could not be referenced in a loop.
Thanks for any help.
 
Put all your textboxes in a panel.

then do

using System.Collections;

public string[] GetValuesFromTextBoxes()
{
ArrayList textarr = new ArrayList();

foreach (Control txt in panel.Controls)
{
if (txt.GetType() == textbox1.GetType())
{
textarr.Add(txt.Text);
}
}

return (string)textarr.ToArray(typeof(string));
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top