Dim iControlIndex As Integer
‘加载控件实例的索引值
Private Sub LoadControl(iIndex As Integer)
Load UserControl11(iIndex)
‘加载控件实例,UserControl11是自定义控件名字
UserControl11(iIndex).Visible = True
End Sub
Private Sub UnloadContr(iIndex As Integer)
Unload UserControl11(2)
‘卸载控件实例,当不使用控件实例时自动卸载
End Sub
Private Sub UserControl11_Done(Index As Integer)
‘根据控件实例的索引决定相应控件实例作出响应
Select Case Index
Case 0
Case 1
......
End Sub
----2.控件在应用程序工具箱(TOOLBOX)中,但没有添加到应用程序中
----这种情况适合于为了完成不同的任务,制作了多个不同功能的控件,在特定条件下只需要一个或几个控件即可。实现方法是将所有可能用到的控件加到VB工具箱中,当应用程序执行时,根据需要由程序自动加载控件。
Dim WithEvents oControl As VBControlExtender
Private Sub LoadControl() ‘加载控件
If Not oControl Is Nothing Then
‘首先判断控件是否存在,若存在则先卸载
Controls.Remove (“szlfPrint1") ‘卸载控件实例
End If
Licenses.Add “test.usercontrol1",
“9CA5E0CC-37D7-11D4-
8547-00A024EE55CD =
uohhlohhhhmoooiouijoohloririmhlhtoqi"
‘如果控件需要许可证码,则需先填加许可证码
Set oControl = Me.Controls.Add(
“test.usercontrol1",“szlfPrint1", Frame1)
‘test.usercontrol是控件的对象编号,
可通过VB的Object Browser得到控件的对象编号,
szlfPrint1是控件的名字,Frame1是控件的容器。
oControl.Visible = True
End Sub
Private Sub UnloadControl() ‘卸载控件实例
If Not oControl Is Nothing Then
Controls.Remove (“szlfprint1")
Set oControl = Nothing
End If
End Sub
Private Sub CmdTest_Click()
‘测试程序,可以像静态加载控件
那样使用控件实例的属性及方法
oControl.Visible = False
oControl.DoMsg
oControl.NodeCode = “dd"
End Sub
Private Sub oControl_ObjectEvent(Info As EventInfo)
Select Case Info.Name
Case “MouseDown"
oControl.DoMsg
Case “click"
MsgBox “Click"
‘双击处理代码
Case Else ‘
End Select
End Sub
----ObjectEvent(Info As EventInfo)能捕捉到控件使用 RaiseEvent产生的所有事件,EventInfo数据结构映射了事件的名称、参数个数和参数的值。
---- 值得注意的是,由于控件在工具箱中而没有在应用程序中,当程序运行时会报错,需把程序的工程属性Remove information about unused Activex Controls 设为不选。
Private Sub RegisterControl_Click
(strControlName As String) ‘注册控件
‘Syspath是regsvr32的目录;
strControlName是控件名字(包括路径)
i=Shell(Syspath & “regsvr32.exe /s /I"
& strControlName, vbHide)
End Sub
Private Sub UnRegisterControl_Click
(strControlName As String) ‘撤消注册控件
i=Shell(Syspath & “regsvr32.exe /s /U"
& strControlName, vbHide)
End Sub End Sub