Question adding and removing controls at runtime

boknat

Member
Joined
Oct 4, 2011
Messages
9
Programming Experience
Beginner
hi, hello, hello, hi,

i am working with my project and i have difficulties in my program.

this is the set-up:
i put a textbox1 and a button1 in the form, textbox1 is for the property of the label text to be add
when i click the button1 i have another textbox, label, and a button, i want the added button to remove the textbox, label, and if possible the button itself,
is there a way to delete a control by the added button?

help please...
thanks in advance
 
Last edited:
If you want to add a control at run time you must first create it by invoking a constructor with the New keyword and then configure it by setting the appropriate properties. You then add it to the desired container (Form, Panel, GroupBox or whatever) by calling the Add method on the Controls collection of that container. If you want to see such code, simply open the designer code file of any form and view the code generated by the designer based on what you do in the visual designer. You can click the Show All Files button in the Solution Explorer to gain access to the designer code file under your form node.

Just as you add a control by calling Add, you remove a control by calling Remove. The Controls collection is just like any other collection in that way. If you don't want to use the control at all after that, make sure that you call its Dispose method too. That will release the control's window handle back to the OS.
 
Back
Top