hello there, i have a problem, it might be trivial but i cannot for the life of me understandw what is causing it.

so i have a numeric updown and based on the value, it loops and creates as many txtboxes as the value in hte numupdown. and then i have a loop that reads the text in these but its not working as intended.

    for (int i = 1; i <= numericUpDown1.Value;i++)
        {
            string textname = "txtName"+i;
            TextBox txtTemp = new TextBox();
            txtTemp.Location = new Point(x,y);
            txtTemp.Name = textname;
            txtTemp.Width = 100;
            grpCol.Controls.Add(txtTemp);

         }

this is the txtbox creating code.

and this is the read loop:

        for(int i =1; i <= numericUpDown1.Value; )
            {
                if (childControl.Name.EndsWith(i.ToString()))
                {
           if(childControl.GetType().ToString().Equals("System.Windows.Forms.TextBox"))
                    {
                        txt2.Text += ((TextBox)childControl).Text;
                    }
                    if (childControl.HasChildren)
                    {
                        IterateThroughChildren(childControl);
                    }
                }
                i++;
            }

and so when i run the code and creat 4 txtboxes like this

  txta1        txtb1
|_______|   |________|

  txta2        txtb2
|_______|   |________|

with the values being:

   1            2

   3            4

i get the output 3124, when i want 1234.
sorry if im being incoherent, but couldnt explain it better.

if you read this far... thank you:P

hello there, i have a problem, it might be trivial but i cannot for the life of me understandw what is causing it.

so i have a numeric updown and based on the value, it loops and creates as many txtboxes as the value in hte numupdown. and then i have a loop that reads the text in these but its not working as intended.

    for (int i = 1; i <= numericUpDown1.Value;i++)
        {
            string textname = "txtName"+i;
            TextBox txtTemp = new TextBox();
            txtTemp.Location = new Point(x,y);
            txtTemp.Name = textname;
            txtTemp.Width = 100;
            grpCol.Controls.Add(txtTemp);

         }

this is the txtbox creating code.

and this is the read loop:

        for(int i =1; i <= numericUpDown1.Value; )
            {
                if (childControl.Name.EndsWith(i.ToString()))
                {
           if(childControl.GetType().ToString().Equals("System.Windows.Forms.TextBox"))
                    {
                        txt2.Text += ((TextBox)childControl).Text;
                    }
                    if (childControl.HasChildren)
                    {
                        IterateThroughChildren(childControl);
                    }
                }
                i++;
            }

and so when i run the code and creat 4 txtboxes like this

  txta1        txtb1
|_______|   |________|

  txta2        txtb2
|_______|   |________|

with the values being:

   1            2

   3            4

i get the output 3124, when i want 1234.
sorry if im being incoherent, but couldnt explain it better.

if you read this far... thank you:P

one question: why do you increment i inside the for loop?

First you have to create a global TextBox variable array, so in the whole class is visible. Then create textboxes, and when you need to loop through them, do the following (code at the bottom):

TextBox[] tbs;

void CreatingTextboxes()
{
    tbs = new TextBox[int.Parse(numericUpDown.Value)];
    //now loop with your code from the top and create textBoxes in the a bit different way they you did:

    int x = 20;
    int y = 30;
    for(int i = 0; i< tbs.Lenght; i++)
    {
         tbs[i] = new TextBox();
         tbs[i].Name = "textBox" + i+1;
         tbs[i].Location = new Point (x, y);
         tbs[i].Width = 100;
         this.Controls.Add(tbs[i]);
         //set new coordinated for a new textbox:
         y += 30; //vertical 
         x += 0;  //horizontal - do what ever you want to put them in the form!
    }
}

void GetTextFromTextBoxes()
{
    StringBuilder sb  = new StringBuilder();
    foreach(Control c in this.Controls)
    {
       if(c is TextBox && c != null)
       {
           Textbox tb = c as TextBox;
           //read each text from textBox:
           if(tb.Text != String.Empty)
               sb.AppendLine(tb.Text);
       }
    }

    //lets show the text in all textBoxes!
    MessageBox.Show(sb.ToString());
    //you can do with the text what ever you want, I only wanted to show how to get all the text from each textBox on the form, and get them together trom all of them!
}

Let me know what do you think,
bye,

BTW: if you want to get the text one beside another - in one line, you only have to change StringBuilder`s AppendLine() method with only Append().
Simple
Enjoy :)

worked flawlessly:) thanks a lot :)

np mate
Iam glad you like it,
and I wrote the code by heart ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.