views:

384

answers:

2

For an unknown reason, VB6 doesn't interact the same way with UserControl than other object.

I have a class that require to hold a graphical interface, a user control and need to be set to be later used from the get method. I have try many thing like using the special class VBControlExtender but without any success.

Here is what I have so far:

Class that hold variables and the user control:

'...
Private WithEvents m_uGUI As VBControlExtender

Public Property Get GUI() As VBControlExtender
 Set GUI = m_uGUI
End Property

Public Property Set GUI(ByVal uValue As VBControlExtender)
 Set m_uGUI = uValue
End Property
'...

Call of the class that cannot compile:

Set myObject.GUI = new ucMyUserControl

Any idea?

A: 

I believe VBControlExtender can only be used with dynamically added controls (i.e. Controls.Add) not intrinsic controls. Why can't you use ucMyUserControl as the type instead?

MarkJ
I cannot use ucMyUserControl because depending of the sentence in the program the user control will be set to ucMyUserControl1 and sometime to ucMyUserControl2. If I change VBControlExtender to UserControl, I still have the same error "invalid use of new KeyWord"
Daok
+5  A: 

From the help on this error (it mentions ListBox and Form, but the same applies to UserControls):

The New keyword can only be applied to a creatable object... You tried to instantiate an Automation object, but it was not a creatable object. For example, you tried to create a new instance of a list box by specifying ListBox in a statement like the following: [sample code snipped] ListBox and Form are class names, not specific object names. You can use them to specify that a variable will be a reference to a certain object type... But you can't use them to instantiate the objects themselves in a Set statement. You must specify a specific object, rather than the generic class name, in the Set statement:

What you want to do is make an array of your UserControls and load new ones as you need them. Set the Index property of your UserControl to 0 to make it an array and then use the Load statement to create new instances:

Load ucMyUserControl(1) 
Set myObject.GUI = ucMyUserControl(1)

When you need more just specify a new upper bound:

Load ucMyUserControl(2) 
Load ucMyUserControl(3)
...

When you're done with them, unload them:

Unload ucMyUserControl(3)
Unload ucMyUserControl(2)
...
raven
+1. Ahhh, it's the New statement with a control type, of course it is. You can't do that in VB6. Nice answer.
MarkJ
I do not see how to make an array of UserControl in a Class and load them to a form when needed. I understand your point of view if I would have to load them directly in a form but I have to hold the usercontrol in a class until the desire moment is trigged.
Daok