|
|
Title | Load an ActiveX control at run time |
Description | This example shows how to load an ActiveX control at run time in Visual Basic 6. The program uses the Controls collection's Add method. |
Keywords | Controls.Add, new control, ActiveX |
Categories | Controls, Tips and Tricks, ActiveX Controls |
|
|
Use the Controls collection's Add method.
First load the SayHello project. From the Project menu, select HelloTools properties. On the General tab, set the ProjectName to HelloTools and click OK. Select the File menu's Make SayHello.ocx command to build the control.
Next load the test program and run it. The following code shows how the test program loads the control.
|
|
Private Sub Command1_Click()
Static i As Integer
Static Y As Integer
Dim ctl As Control
Set ctl = Controls.Add( _
"HelloTools.SayHello", _
"SayHello" & Format$(i), Me)
ctl.Move 0, Y
ctl.Visible = True
i = i + 1
Y = Y + ctl.Height + 120
End Sub
|
|
Robert Heinig (rheinig-beikuk@gmx.net) found a problem trying to load an Acrobat Reader ActiveX control. Here's the code he used:
|
|
' For this sample to work, do one of the following:
' a) Comment out the pdf.LoadFile call
' - or -
' b) include the "Acrobat Control for ActiveX"
' in your toolbox (comes with the Acrobat Reader),
' And turn the Project Option "remove unused ActiveX..."
' off.
' Plus, You need any Acrobat 4.0x reader installed.
' My guess is the cause is the difference between
' the names "PdfLib.Pdf" (Typelib name, Class name)
' and "Pdf.PdfCtrl.1" (the ProgID), VB just doesn't
' call QueryInterface correctly unless it already
' knows about the ProgID/Class name correlation!
' There's probably a workaround, but I can only think
' of a vtable hack right now - a bit over the top for this.
Dim pdf As Control
' Same behaviour if declared as PdfLib.Pdf, Control or
' Object.
Private Sub Command1_Click()
Dim v As Variant, sFile As String
Set pdf = Controls.Add("Pdf.PdfCtrl.1", "pdf")
pdf.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
Command1.Visible = False
pdf.Visible = True
For Each v In Array("Readme", "Liesmich", "Leame", _
"Lisezmoi")
sFile = "C:\Progra~1\Adobe\Acroba~1.0\Reader\" & v _
& ".pdf"
If Len(Dir$(sFile)) > 0 Then
pdf.LoadFile sFile
Exit For
End If
Next
End Sub
|
|
|
|
|
|