Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Forms
Is it possible to create textboxes @ runtime based on user input
 
1)textbox--- to get the user input.
2)button--- to create the textboxes
 
i have a textbox for getting user input...if user give 2 means then i want to create dynamically two textboxes
 
Thanks in advance....
Posted 25-Feb-13 3:56am
Edited 25-Feb-13 4:13am
v3
Comments
Orcun Iyigun at 25-Feb-13 9:01am
   
Tag it correctly! Win Forms,web,mvc what is it?
GopinathSk at 25-Feb-13 9:04am
   
sorry....i am new to here...anyway thanks...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Sure.
TextBox txt = new TextBox();
txt.ID = "txtNew";
txt.Text = "This is a new textbox";
Form.Controls.Add(txt);
 
That's a simple example but you can adapt to your needs.
  Permalink  
Comments
GopinathSk at 25-Feb-13 9:04am
   
hey i want not like this....my pblm is when i give 2 as user input .....it create 2 text boxes at runtime.........
ThePhantomUpvoter at 25-Feb-13 9:05am
   
Then write the code that will do that. You only asked if it was possible and the answer is "yes"
ryanb31 at 25-Feb-13 9:08am
   
Can you clarify? I do not understand your problem.
GopinathSk at 25-Feb-13 9:13am
   
i have a textbox for getting user input...if user give 2 means then i want to create dynamically two textboxes
ryanb31 at 25-Feb-13 9:37am
   
OK, so one will have an id of txt1 and the other txt2, for example.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
Try this:
 
private void button1_Click(object sender, EventArgs e)
{
    AddDynamicTextBox(5);
}
 
public void AddDynamicTextBox(int numberOfTextBoxes)
{
    for (int i = 0; i <= numberOfTextBoxes; ++i)
    {
        TextBox textBox = new TextBox();
        textBox.Name = "TextBox" + i.ToString();
        textBox.Text = "TextBox" + i.ToString();
        textBox.Location = new Point(20, i * 20);
        this.Controls.Add(textBox);
    }
}
 
The "numberOfTextBoxes" you will defined from your input. One thing you need to be careful of is the location, if you do not give location value, the control will add each text box on top of each other so it will look like there is only one text box.
 
Regards
Jegan
  Permalink  
v2
Comments
Jayanta Chatterjee at 25-Feb-13 12:17pm
   
My 5..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Use the on button Click event
 
TextBox TextBox1 = new TextBox();
TextBox1.ID = "txt_1" ;

TextBox1.MaxLength = 50;
 
//Add Style
TextBox1.Style.Add("width", "200px");

TextBox1.Width = 50;
this.Controls.Add(TextBox1);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

private void button1_Click(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
Form form1 = new Form();
form1.Controls.Add(txtBox);
}
  Permalink  
Comments
ThePhantomUpvoter at 25-Feb-13 12:45pm
   
This is not an answer to your question. And this code would not work anyway. You are simply creating a new TextBox, then creating a new instance of Form, adding the newly created TextBox to your newly created Form and then throwing it all away as your button click handler ends and the local variables that you created go out of scope.

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
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