How to pass a UserControl as an argument?

sputnik

Member
Joined
Oct 11, 2005
Messages
10
Programming Experience
5-10
I've got several user controls and every time I add them, I post this code:
Dim uc As New ucTemplates ' 'ucTemplates' is the name of the actual user contrll
gbControls.Controls.Clear()
gbControls.Controls.Add(uc)

So, I'd like to create a sub that does the same thing, passing the usercontrol as an argument

Private Sub AddUC(myControl as UserControl)
gbControls.Controls.Clear()
gbControls.Controls.Add(myControl)
End Sub

But - when I try:
addUC(ucTemplates)
I get an error that ucTemplates is a type and cannot be used as an expression
I can get it working if I add the Dim uc As New before the addUC - but I'd rather have it so I only repeat one line, not 2

How can this be accomplished, then?
 
Last edited:
Dim uc As New ucTemplates ' 'ucTemplates' is the name of the actual user contrll
addUC(ucTemplates)
You still have to create an instance of the control like you previously did using the New keyword.
If your 'client' code has nothing to actually add before passing the control instance to the method you can drop the parameter and let the method create the control instance also.
 
Back
Top