Referencing User Controls

AspireTech

New member
Joined
Oct 21, 2007
Messages
1
Programming Experience
3-5
Does anyone know how I can reference a UserControl using a string value? Here is what I am trying to do.

I need to be able to add a usercontrol to a form when a button is pressed, all of the form controls are being built at runtime based on records in an SQL table.

Below is the code I'm using to open the new user control. I need to be able to specify which UserControl to open based on an SQL record.

Thanks.

-Scott


Dim opnMenu As New UserControl
//where UserControl is the name of my actual control, from example MainMenu//

Me.Parent.Controls.Add(opnMenu)
opnMenu.Show()
Me.Parent.Controls.Remove(Me)
 
It's called reflection. You would use Type.ReflectionOnlyGetType to create a Type class representing your UserControl's type. You then use members of that Type object to manipulate the type. You would use InvokeMember to invoke the type's constructor with no arguments to create an instance.

Note that once you start to use reflection you have to continue to do so. What I means is that if you don't know the type at design time to create the object then you don't know the type at design time to access the object's members either. You can cast the object reference and assign it to a variable of type Control, UserControl or maybe some base class or interface of your own definition, but you can NOT ever use a variable of the object's actual type.
 
Back
Top