Generate TextBox Dynamically at Runtime in Windows Form Application

Generate TextBox Dynamically at Runtime in Windows Form Application

We generate a textbox at the runtime. We give the quantity of the textbox that we want to generate in the TextBox. It will generate the textbox.

Open visual studio -> File -> New -> Project -> WindowsFormApplication

Drag panel1, panel2, textBox1 and button from a toolbox window

Now, Double click on the click me button and write the following code:

try
{
int txtno = int.Parse(txt1.Text);
int pointX = 30;
int pointY = 40;
panel2.Controls.Clear();
for (int i = 0; i < txtno; i++)
{
TextBox a = new TextBox();
a.Text = (i + 1).ToString();
a.Location = new Point(pointX, pointY);
panel2.Controls.Add(a);
panel2.Show();
pointY += 20;
}
}
catch (Exception)
{
MessageBox.Show(e.ToString());
}

Press F5 and run. Enter the no in the textbox, and it will generate the textbox.

Leave a Reply

Your email address will not be published. Required fields are marked *