Hey, in class the teacher told us to do some programs, some of them with many TextBox and I tried hard to read the TextBoxes and save them onto a array inside a for, so that I didn't had to assign every single one of them manually, but in the end that was the result, a very stupid way of programming I think, I asked my teacher and even she could come up with a way of doing that...
All the textboxes are named "txt*" were * is 1, then 2, 3.... You get the idea, so I thought, well gonna save a string were it was "txt" + x(variable that is incremented) + ".Text"(Because if I don't put a real TextBox name it doesn't recognise it, if instead of putting txt1 I put txtx, txt(x), txt + x or other way in the end I couldn't put .Text, so I made that string) but without result, as it didn't accept the string.
For Example:
Instead of doing:
Vec[0] = Convert.ToInt32(txt1.Text);
Vec[1] = Convert.ToInt32(txt2.Text);
Vec[2] = Convert.ToInt32(txt3.Text);
Vec[3] = Convert.ToInt32(txt4.Text);
Vec[4] = Convert.ToInt32(txt5.Text);
I want some cicle that could do something like:
int x = 1;
for (int i = 0; i < 5; i++)
{
Vec[i] = Covert.ToInt32(txt(x).Text);
x++;
}
Or instead a way to convert a string to the TextBox name would be appreciated...
I couldn't find a way to do something like that, please help me...
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Sep 15, 2009, 6:45 PM EDT
Here is Code for your Logic :)
dont Forget to mark "Do you like answer" it will give me some credits :)
private void button1_Click(object sender, EventArgs e)
{
int ArrSize =4;
string[] x = new string[ArrSize];
int i;
List<TextBox> txtList = new List<TextBox>();
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
txtList.Add((TextBox)c);
}
}
i = ArrSize-1;
foreach (TextBox t in txtList)
{
x[i] = t.Text;
i--;
}
}
Roei BarRank #1,271 · 1.4K reputationPosted Sep 15, 2009, 9:55 PM EDT
private void button1_Click(object sender, EventArgs e) { ListString> txtStrings = new ListString>();List<TextBox> txtList = new List<TextBox>();
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
txtList.Add((TextBox)c);
}
}
}
Ricardo Cunha9 reputationPosted Sep 17, 2009, 1:38 PM EDT