Hi,
I am trying to get TextBox. Whenever I click on the button I should get a TextBox. Here is my code. But I am getting only one TextBox.
Collapse

    private void button1_Click_1(object sender, EventArgs e)
    {
        txtRun = new TextBox();
        txtRun.Name = "txtDynamic";
        txtRun.Location = new System.Drawing.Point(20, 18);
        txtRun.Size = new System.Drawing.Size(200, 25);
        // Add the textbox control to the form's control collection
        this.Controls.Add(txtRun);
    }

Any help would be appreciated.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi, there

int c = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            TextBox txtRun = new TextBox();
            txtRun.Name = "txtDynamic"+c++;
            txtRun.Location = new System.Drawing.Point(20, 18+(20*c));
            txtRun.Size = new System.Drawing.Size(200, 25);
            this.Controls.Add(txtRun);
        }

The problem with your code is that it creates the TextBoxes with the same properties whenever you click. To fix this let's introduce a counter which will be increased whenever the button is clicked. This way the new textbox's name will always end with a number higher then the previous and it's location will go down with 20 pixels

txtRun.Location = new System.Drawing.Point(20, 18+(20*c));

Hope this helps, if this solves your problem, please mark this thread as solved.

This question has already been answered. Start a new discussion instead.