Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Whats the code to add objects like a new textbox, e.g, textbox1 textbox2 textbox3 textbox4 etc etc... each time i press a button can anyone help?
Posted

1 solution

At the click event of the button do the below :

C#
TextBox myNewtextbox=new TextBox();
pnl.Controls.Add(myNewtextbox);
//where pnl is the panel where you want to add your textbox
 
Share this answer
 
v2
Comments
Olivier Levrey 16-Mar-11 7:20am    
Correct. Voted 5. You need to set the Location property to place your control, the Size one for its size and so on.
Tarun.K.S 16-Mar-11 7:53am    
Thanks Olivier! Yes that's true the OP has to set more properties to place it correctly in the form.
CodeHawkz 16-Mar-11 7:42am    
What Tarun suggested is indeed the correct way to do it.

Just on a side note.. You can see how .NET adds controls to your form and what properties it sets, if you look into the designer.cs of a specific form.. If you don't know how to find that, just double click your form GUI, type the name of some control you have already added to the form and right click on it, and click "Definition".. This will take you there :)

Hope this helps
Tarun.K.S 16-Mar-11 7:54am    
Thanks Hawkz!
Sergey Alexandrovich Kryukov 16-Mar-11 14:15pm    
Well, what else? May be adjust docking, padding, top, left, width... OP guesses... :-) A 5.
--SA

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