Tools Links Login

Dynamic(truly late-bound) loading of controls

This is a technique for late-binding to controls on your forms or usercontrols. Other techniques that I've seen on this web-site involve using control arrays. But in order to do this you need to include a reference to the control from the components menu. This is called early-binding and can disasterous for your programs if you're utilizing a COM object that frequently changes interfaces. The late-binding technique requires only the ProgID of the object as listed under HKEY_CLASSES_ROOT in your registry.

Original Author: Zac Spery

Code


Private Sub Form_Load()
Me.Show 'if you try to add a control to a form that's
'not yet visible...kaboom
Set objControl = Me.Controls.Add("MyControl.usercontrol1", "MyTextBox")
'the first parameter is the progid, the second is merely
'the name you'll use to identify it in the control collection
objControl.Visible = True
objControl.Top = 0
objControl.Left = 0
'all methods/properties except the basic ones like
'visible, top, width, etc, must be called like so:
objControl.object.MyProperty = "test"
objControl.object.MyMethod
End Sub
Private Sub Form_Unload(Cancel As Integer)
'be sure to clean up after yourself or you might
'start getting GPFs.
Call Me.Controls.Remove("MyTextBox")
Set objControl = Nothing
End Sub
You can also load VB control with this method.
Look in the object browser under the "VB" library
and you'll the progid's for some of VB's
controls.
In the above example, if you supply a progid of
VB.TEXTBOX then it will load a regular textbox
onto the screen.
Of course, this provides you with no way to
capture events. In order to do this, you'll need
to change the dim statement for the object. Try
this:
Dim WithEvents objControl As VBControlExtender
and you can capture events like so:
private sub objControl_MyEvent()
msgbox "MyEvent triggered"
end sub
Unfortunately, when you try to set your
VBControlExtender to a VB control (like
VB.TEXTBOX), you get a type mismatch error. I
haven't figured out how to do this yet.

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 105 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.