-1

Possible Duplicate:
Find a control in C# winforms by name

I will try to explain it.. So I have a for loop, and each time I want to load the textbox number equal with the cycle integer.

Here is my code:

for(int i=1 ; i<=indulok+1 ; i++)
{
    StreamWriter sw = new StreamWriter("futamok/"+comboBox1.Text + "/" + indulok + ".txt");
    sw.WriteLine(textBox1.Text);
    sw.Dispose();
}

Now I want to replace textBox1.Text to something like this:

sw.WriteLine(textBox + i + .Text);

But this is not working of course.

Community
  • 1
  • 1
user1973121
  • 3
  • 1
  • 1
  • 2

3 Answers3

6

Here is an alternative that would find the control by name.

        for (int i = 1; i <= indulok + 1; i++)
        {
            if (this.Controls.ContainsKey("textBox" + i.ToString()))
            {
                TextBox txtBox = this.Controls["textBox" + i.ToString()] as TextBox;
                if (txtBox != null)
                {
                    string FileName = "C:\\" + i.ToString() + ".txt";
                    System.IO.File.WriteAllText(FileName, txtBox.Text);
                }
            }
        }

If your control is nested inside other controls (ie. a GroupBox), you may need to use the Find function

        for (int i = 1; i <= indulok + 1; i++)
        {
            Control[] controls = this.Controls.Find("textBox" + i.ToString(), true);
            if (controls.Length > 0)
            {
                TextBox txtBox = controls[0] as TextBox;
                if (txtBox != null)
                {
                    string FileName = "C:\\" + i.ToString() + ".txt";
                    System.IO.File.WriteAllText(FileName, txtBox.Text);
                }
            }
        }
Hagelt18
  • 828
  • 1
  • 10
  • 21
  • I am sorry my question is misunderstandable. So I dont want to change the textbox's texts, I want to save the serveral textbox texts to serveral files ( textbox1.text to 1.txt, textbox2.text to 2.txt ) – user1973121 Jan 24 '13 at 20:42
  • I've updated the solution to write the values to their own text files. I put them in C:\1.txt in the sample but you can change the path to anything you want – Hagelt18 Jan 24 '13 at 20:49
  • This second code doesn't save anything at all :/ – user1973121 Jan 24 '13 at 20:57
  • I'm sorry, it was Controls.Find("txtBox" + i.ToString(), true) but it was supposed to be Controls.Find("textBox" + i.ToString(), true) I've updated the code to reflext this. – Hagelt18 Jan 24 '13 at 21:04
  • Thank you very much, now its working! I will be a programmer, maybe 10 years later! :D – user1973121 Jan 24 '13 at 21:09
3

You can put your textboxes into some sort of indexed collection. the simpelest of which is an array

so, assume you had some array like

textBox[] textBoxes

then you can call

sw.WriteLine(textBoxes[i].text);
0

I guess you are running some sort of Windows Forms-project? If you are, you can loop the Controls-array to find all textboxes:

foreach (Control c in this.Controls)
{
    if (c.GetType() == textBox1.GetType())
        sw.WriteLine(c.Text);
}
Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51