Click here to Skip to main content
12,452,721 members (52,176 online)
Rate this:
 
Please Sign up or sign in to vote.
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:10am
Updated 21-Sep-12 1:02am
_Amy56K
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100