在设计者的基础上可以创建可视的界面。扩展模型可以视为空的插槽,在该插槽中可以插上不同类型的设计者。
Visual Basic 开始仅使用一种设计者。然而,现在 Visual Basic 已成为多种设计者的宿主,其中包括 UserDocuments 和 UserControls,它们允许创建 ActiveX 文档和 ActiveX 控件,这些文档和控件不仅能用于 Visual Basic,而且能用于诸如 Microsoft Word 或者 Microsoft Excel 之类的 Microsoft Office 应用程序。
下面的代码片断显示了如何引用 VBForms 对象,以及其它扩展性对象:
'
给窗体添加控件。所添加控件的类型基于在组合框中选择的控'
件类型。Private Sub cmdAddControl_Click()
Dim c As VBComponent
Dim p As VBProject
Dim vbc As VBControl
Dim vbc2 As VBControl
Dim vbf As VBForm
Dim sc As String
Dim sp As String
Dim svbc As String
Dim pid As String
sp = cmbProj.Text
sc = cmbComp.Text
svbc = cmbControls.Text
If sp <> "" And sc <> "" And svbc <> "" Then
Set p = vbi.VBProjects.Item(sp)
Set c = p.VBComponents.Item(sc)
If c.Type = vbext_ct_VBForm Then
Set vbf = c.Designer
Set vbc = vbf.VBControls.Item(svbc)
pid = vbc.ProgId
Set vbc2 = vbf.VBControls.Add(pid)
End If
End If
End Sub
'
刷新窗体中的控件列表。当添加控件时,主对象中的事件处'
理程序调用子过程。Public Sub RefreshControls()
Dim c As VBComponent
Dim p As VBProject
Dim vbc As VBControl
Dim vbf As VBForm
Dim sc As String
Dim sp As String
Dim tempIndex As Long
Screen.MousePointer = vbHourglass
If cmbControls.ListCount > 0 Then
tempIndex = cmbControls.ListIndex
'
用来恢复先前选定的临时索引。End If
sp = cmbProj.Text
sc = cmbComp.Text
If sc <> "" And sp <> "" Then
cmbControls.Clear
Set p = vbi.VBProjects.Item(sp)
Set c = p.VBComponents.Item(sc)
If c.Type = vbext_ct_VBForm Then
c.Activate
Set vbf = c.Designer
For Each vbc In vbf.VBControls
cmbControls.AddItem _
vbc.Properties("name")
Next vbc
Else
cmbControls.Text = "No Form Selected"
End If
'
恢复先前的选定。If cmbControls.ListCount > 0 Then
If tempIndex <= cmbControls. _
ListCount - 1 Then
cmbControls.ListIndex = tempIndex
Else
cmbControls.ListIndex = 0
End If
End If
End If
Screen.MousePointer = vbDefault
End Sub
'
从窗体中删除控件Private Sub cmdRemoveControl_Click()
Dim c As VBComponent
Dim p As VBProject
Dim vbc As VBControl
Dim vbf As VBForm
Dim sc As String
Dim sp As String
Dim svbc As String
sp = cmbProj.Text
sc = cmbComp.Text
svbc = cmbControls.Text
If sp = "" Or sc = "" Or svbc = "" Then Exit Sub
Set p = vbi.VBProjects.Item(sp)
Set c = p.VBComponents.Item(sc)
If c.Type = vbext_ct_VBForm Then
Set vbf = c.Designer
Set vbc = vbf.VBControls.Item(svbc)
vbf.VBControls.Remove vbc
End If
End Sub