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.

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.

Thanks a lot..Its working now....

Good Job Mr. CloneXpert.
Thanks a lot ... Its help me too.. :)

Thanks

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.