|
-
Apr 19th, 2002, 08:31 AM
#1
Thread Starter
New Member
Dynamically loading an ActiveX Control
Can anyone help me to dynamically set a reference to an ActiveX control and disply the control on my form.
I have created two ActiveX controls, I will call them myControl1 and myControl2, which work fine when I drop them directly onto my form at design time. I can access their properties and respond to their events.
However I wish to display only one of the controls at runtime, and which one will depend upon how the form is accessed.
I have tried declaring an object variable of type myControl1/myControl2 and then using this to access the controls properties. Doing this I get a runtime error indicating that the object doesn't exist.
Can anyone tell me what I am doing wrong/what I need to do in order to be able to add the control to the form at runtime rather than at design time.
-
Apr 19th, 2002, 09:39 AM
#2
did you try late binding?
Code:
Dim ctl as Control
If x=1 then
Set Ctl = CreateObject("MyControl1")
End if
if x =2 then
Set Ctl = CreateObject("MyControl2")
End if
-
Apr 21st, 2002, 06:12 AM
#3
Hyperactive Member
If you're going to do as jim suggested, you'll need to set a reference to the OCXs... goto Project-->References and find your controls.
But why not just have one of each on there and make them invisible?
-
Apr 24th, 2002, 03:48 PM
#4
Thread Starter
New Member
Jim, I tried your suggestion but got the following message
Runtime error '429'
ActiveXcomponent can't create object.
Any ideas.
ChiefRB - The reason I don't want to drop the controls onto the form at design time, making them invisible, is that I don't actually know how many of these controls I may need. Otherwise that was a good solution.
Last edited by OwB; Apr 24th, 2002 at 03:51 PM.
-
Apr 24th, 2002, 05:15 PM
#5
You can give the invisible one an index making it the start of a control array which makes it easier to add to later...OR...make sure that you also uncheck the 'Remove information about unused controls' in the Project properties and try Jim's code again.
If you reference a control but don't use it (place it on any forms) with that checked then it effectively makes it like you didn't reference it in the first place.
OR it may just be that CreateObject needs the Library name as well as the control name (i.e. MSComctlLib.TreeView=Treeview)
-
Apr 25th, 2002, 02:30 PM
#6
Thread Starter
New Member
Sussed it,
'Add the control to the forms control collection
Form1.Controls.Add "project.controlName", "MyControl"
'Set a reference to an object variable in order to reference the
'control
Dim myControlRef As controlName
Set myControlRef = Form1.Controls("MyControl")
'Now use the object variable to set the control properties
myControlRef.Visible = True
With a bit of clever adaptation the above should be capable of adding numerous controls at run time.
Thanks for the pointers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|