Share via


Visual Basic Concepts

Manipulating Forms and Controls with Add-Ins

A designer is a base on which you create your visual interface. You can think of it in the extensibility model as an empty socket which you can plug in different designer types.

From its beginnings, Visual Basic used one kind of designer. Now, however, Visual Basic can host other kinds, including UserDocuments and UserControls that allow you to create ActiveX documents and ActiveX controls that can be used not only in Visual Basic, but in Microsoft Office applications such as Microsoft Word or Microsoft Excel.

The following code fragments demonstrate how to reference the VBForms object, as well as other extensibility objects:

' Add controls to form. Type of controls to add is
' based on the type of control selected in ComboBox.
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

' Refresh the list of controls on a form. Sub is called
' from event handler in the main object when controls
' are added.
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
      ' Temp index to restore prior selection
   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
         ' Restore the prior selection.
         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

' Remove control from form
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