Click here to Skip to main content
Sign Up to vote bad
good
See more: C#.NET
Below is my code to add control. it works but only for 1st click..
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 21 Sep '12 - 0:10
Edited 21 Sep '12 - 1:02
_Amy38.5K

Comments
Kenneth Haugland - 21 Sep '12 - 6:12
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

4 solutions

hii,
just
change z+25 to z+=25 and define x and z globally it will run successfully.
 
modified code
 
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);
       }
   }
  Permalink  
Comments
mane0806 - 23 Sep '12 - 9:27
in my code x and z are defined globally. i got by my own.bt thanks for your help
Have you heard about loops?
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);
}
  Permalink  
Hi,
See you code below:
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:
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
  Permalink  
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!
Smile | :)
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 479
1 Arun Vasu 253
2 OriginalGriff 210
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,129
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 2,999


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid