Question Dynamically Load UserControl to Panel

Mica77

Member
Joined
Jan 14, 2009
Messages
5
Programming Experience
1-3
Hi, I am trying to load a usercontrol.vb to a panel in form1

I have been unable to achieve this.

Could you please assist where posible.

Usercontrol = Control1.vb

Form = Form1
Panel = Panel1

Thankyou
Michael
 
A UserControl is just a control like any other. To create a control in code you do the same as you do for any other class: you use the New keyword to create a new instance. You would then set the appropriate properties of that instance. In the case of a control that might include the Location and the Size. Once you've done that you Add the instance to the Controls collection of the intended container, which is the Panel in your case.

If you want to see an example of how controls are added in code then you can just look at how the designer does it. All the actions you perform in the designer are converted to code, which gets executed when you run your project. To see that just open the Solution Explorer and click the Show All Files button. Expand your form's node and open the designer.vb file. That contains all the code to create, configure and add the controls that you created in the designer. You just use similar code but in the user code file.
 
Back
Top