Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
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.
Posted
Updated 24-Aug-10 22:28pm
v2

Your text boxes are all in the same place, and all have the same name. You're either getting many text boxes that are all in the same place, so they cover each other, or because the name is the same, your new one is replacing your old one.
 
Share this answer
 
Comments
neetika 2010 25-Aug-10 5:33am    
Hi,

what should i do to avoid that....
DavidKiryazi 25-Aug-10 5:42am    
You need to somehow change the properties that Christian indicated were conflicting. ie you need to dynamically change the location.

You could use member variables initialised to some value eg. x = 10, y = 10 then:

x += 10;
y += 10;
System.Drawing.Point(x, y);

Dave
Dalek Dave 25-Aug-10 6:29am    
I would also append the name with a counted variable...txtDynamic1,txtDynamic2 etc
neetika 2010 25-Aug-10 6:37am    
Hi,
I have added the same ..but iam getting second text box in the middle of the first textbox..i have to get one after the another in next line with some space..


Any help would be appreciated.
DavidKiryazi 25-Aug-10 7:49am    
neetika you need to do some maths, the reason you are getting that is because 10 is just a random number i pulled out of my... hat.

A quick fix: Instead of using the values 10 that I used, if you want to put a textbox below another one, add the height of the textbox + 2 pixesl so you get y = textbox.Height + 2; and if you wanna put it next to the other one use x = textBox.Width + 2;

You need to remember that the location property is taken with respect to the top left hand corner of the form (0,0).
;) ;) ;) ;) ;) ;) you should change your textboxs location after cliking button . all location should be diffrent.:cool::cool::cool::cool:
like this

string x=40;
private void button1_Click_1(object sender, EventArgs e)        {            
           txtRun = new TextBox();            
           txtRun.Name = "txtDynamic";            
           txtRun.Location = new System.Drawing.Point(x+20,18);
           x+=10;
           txtRun.Size = new System.Drawing.Size(200, 25);            
           this.Controls.Add(txtRun);        
}
 
Share this answer
 
v2
Comments
Dalek Dave 25-Aug-10 6:28am    
The answer is correct, but way too many smileys! :)
T.Saravanann 25-Aug-10 6:40am    
Reason for my vote of 4
Its Correct Answer.But small change
'x' is not a string its Integer. :)
Dalek Dave 25-Aug-10 7:25am    
well spotted
Hi neetika2010,

Just set 'y' value in Location Point.
C#
//Assign this Value in Global
int x=100;
int y=25;

// In Your Button Event
private void button1_Click_1(object sender, EventArgs e)        {
           txtRun = new TextBox();
           txtRun.Name = "txtDynamic";
           txtRun.Location = new System.Drawing.Point(x,y);           
           txtRun.Size = new System.Drawing.Size(200,25);
           y=y+25;  
           this.Controls.Add(txtRun);
}


If you need a large space in between a Textbox just add a value in y.
ex: y=y+35;

In my Suggestion using UserControl you can create any control one after another.

Thanks :)
 
Share this answer
 
v2
Hi,

Use grid and bind textbox inside grid. it will be easy to add dynamic textbox.
 
Share this answer
 
Comments
DavidKiryazi 25-Aug-10 7:51am    
Toni is the grid static though or can you keep adding rows and columns?

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