Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code to add control. it works but only for 1st click..
C#
int x=27, z=65;
        private void button1_Click(object sender, EventArgs e)
        {
            TextBox textBox2 = new TextBox();
            textBox2.Name = "textBox2";
            textBox2.Location = new Point(x,z+25);
            textBox2.Visible = true;
            this.Controls.Add(textBox2);
        }

I want to add multiple text box to my form using same click event..
thanks in advance
Posted
Updated 21-Sep-12 1:02am
v2
Comments
Kenneth Haugland 21-Sep-12 6:12am    
Think your problem is here:
textBox2.Name = "textBox2";
create a couter that gives all of the controls a unique name.
And you might want to us mouse down and position to place your control or something like that

hii,
just
change z+25 to z+=25 and define x and z globally it will run successfully.

modified code

C#
public partial class Form1 : Form
   {
       int x = 27, z = 65;
       public Form1()
       {
           InitializeComponent();
       }
       private void button1_Click(object sender, EventArgs e)
       {
           TextBox textBox2 = new TextBox();
           textBox2.Name = "textBox2";
           textBox2.Location = new Point(x, z += 25);
           textBox2.Visible = true;
           this.Controls.Add(textBox2);
       }
   }
 
Share this answer
 
v2
Comments
mane0806 23-Sep-12 9:27am    
in my code x and z are defined globally.
i got by my own.bt thanks for your help
Have you heard about loops?
C#
int i = 1, x=27, y=65;
for (i = 0; i < 5; i++ )
{
  TextBox tb = new TextBox();
  tb.Name = "textBox" + i.ToString();
  tb.Location = new Point(x, y + 25 * i);
  tb.Visible = true;
  this.Controls.Add(tb);
}
 
Share this answer
 
Hi,
See you code below:
C#
int x=27, z=65;
private void button1_Click(object sender, EventArgs e)
{
    TextBox textBox2 = new TextBox();
    textBox2.Name = "textBox2";
    textBox2.Location = new Point(x,z+25);
    textBox2.Visible = true;
    this.Controls.Add(textBox2);
}

You are adding the code dynamically, correct. Your problem is, you are adding the controls in the same location again and again. Try putting the different location for the buttons each time you are adding. Try this:
C#
int x=27, z=65;
private void button1_Click(object sender, EventArgs e)
{
    TextBox textBox2 = new TextBox();
    textBox2.Name = "textBox2";
    textBox2.Location = new Point(x,z+25);
    textBox2.Visible = true;
    this.Controls.Add(textBox2);
    z += 20;
    y += 20;
}



Hope it helps.
--Amit
 
Share this answer
 
Your code is correct
control is added on every click but it's overlapping due to location property is every time same.
so, it's look like no new control added
just location should be change
Happy coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900