I made this code to generate a textbox when I click on the button the problem is that I want to read text from those textboxes as make a arraylist that receive the texts from those text boxes

private int count=0;
private void New_Click(object sender, EventArgs e)
        {
            TextBox tb = new TextBox();
            tb.Name = "Process" + count;
            tb.Location = new Point(13, y);
            tb.Size = new Size(123, 29);
            tb.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.Controls.Add(tb);
        }

Hi,

Try this code:

for (int i = 0; i < this.Controls.Count; i++)
 { if (this.Controls[i] is TextBox)
       strList.Add(this.Controls[i].Text); 
 }

But I make more than text box and I want to make an arraylist to tb and another array list to tb1

private int count=0;
private int y=10;
private void New_Click(object sender, EventArgs e)
        {
            TextBox tb = new TextBox();
            tb.Name = "firstname" + count;
            tb.Location = new Point(13, y);
            tb.Size = new Size(123, 29);
            this.Controls.Add(tb);

            TextBox tb1 = new TextBox();
            tb1.Name = "lastname" + count;
            tb1.Location = new Point(154, y);
            tb1.Size = new Size(123, 29);
            this.Controls.Add(tb1);

y++;
        }

then, check control's name to see if it is a firstname or lastname textbox .

for (int i = 0; i < this.Controls.Count; i++) 
{
      if (this.Controls[i] is TextBox)       
           {
              if (this.Controls[i].Name.Contains("firstname"))
                 strListFName.Add(this.Controls[i].Text);  
              if (this.Controls[i].Name.Contains("lastname"))
                 strListLName.Add(this.Controls[i].Text);  
            }
}

It is not a good practice to make two lists of thing that belong together, like firstname and lastname. Make a list of a struct or class which contains both(perhaps with other info)

commented: best practices +6

there is a error out of range

Check the value of array....

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.