Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 25-Feb-13 3:13am
v3
Comments
Orcun Iyigun 25-Feb-13 9:01am    
Tag it correctly! Win Forms,web,mvc what is it?
GopinathSk 25-Feb-13 9:04am    
sorry....i am new to here...anyway thanks...

Sure.
C#
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.
 
Share this answer
 
Comments
GopinathSk 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.........
[no name] 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"
ZurdoDev 25-Feb-13 9:08am    
Can you clarify? I do not understand your problem.
GopinathSk 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
ZurdoDev 25-Feb-13 9:37am    
OK, so one will have an id of txt1 and the other txt2, for example.
Hi,
Try this:

C#
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
 
Share this answer
 
v2
Comments
JayantaChatterjee 25-Feb-13 12:17pm    
My 5..
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);
 
Share this answer
 
v2
private void button1_Click(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
Form form1 = new Form();
form1.Controls.Add(txtBox);
}
 
Share this answer
 
Comments
[no name] 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)



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