Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add a usercontrol in VB.net

I have made a custom UserControl i Vb.net (windows application).

How can I add this dynamically to a form?

like image 265
eflles Avatar asked Dec 01 '22 12:12

eflles


1 Answers

A UserControl is essentially just another class. It inherits from Control, so you can do all kinds of things you do with controls, but otherwise it's just a class. Thus, to add the usercontrol dynamically to your form you'd do the following:

  1. Create a new instance of your control. Like Dim X As New MyControl()
  2. Add the control to your form as a child object to whatever container you want it. Like Me.MyGreatTabPage.Controls.Add(X). You can also add it directly to your form too, because a form is also a container.
  3. Set the controls position within the container. That would be setting X.Location and X.Size.

Remember that each instance you create with New MyControl() will be a separate MyControl. Don't make the mistake of repeatedly creating new controls and placing them over each other somehow. Create and place the control once. Assign it to a member variable to your form, and when you need to work with it, use this variable.

like image 186
Vilx- Avatar answered Dec 03 '22 03:12

Vilx-