Share via


Adding List Boxes and Combo Boxes (Visual Basic 6.0 Tutorial) [Office 2003 SDK Documentation]

Previous  Adding Check Boxes

The following steps show you how to add a list box to the SimpleSample smart document. This is the same basic process that you use to add a combo box.

  1. The first thing you need to do is add a constant for the listbox element in the SimpleSample schema. Insert the following code into the general declarations section of your code module below the existing constants.

    Const cLIST As String = cNAMESPACE & "#listbox"
    
  2. Next, you need to add 1 to the cTYPES constant. Remove the existing cTYPES constant, and enter the following code or change your code to match.

    Const cTYPES As Integer = 7
    
  3. Now you are ready to modify the existing code to insert the list box. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.

    In the SmartDocXMLTypeName property subroutine, insert the following code.

            Case 7
                ISmartDocument_SmartDocXmlTypeName = cLIST
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

            Case 7
                ISmartDocument_SmartDocXmlTypeCaption = "List box"
    

    In the ControlCount property subroutine, insert the following code.

            Case cLIST
                ISmartDocument_ControlCount = 1
    

    In the ControlID property subroutine, insert the following code.

            Case cLIST
                ISmartDocument_ControlID = ControlIndex + 600
    

    In the ControlTypeFromID property subroutine, insert the following code.

            Case 601
                ISmartDocument_ControlTypeFromID = C_TYPE_LISTBOX
    

    In the ControlCaptionFromID property subroutine, insert the following code.

            Case 601
                ISmartDocument_ControlCaptionFromID = _
                    "Select your favorite baseball team."
    
  4. The PopulateListOrComboContent method specifies the value of the items in the list. This list is created similarly to radio buttons. The following code specifies the Count parameter; the List parameter specifies the value of each of the five items in the list.

        Select Case ControlID
            Case 601
                Count = 5
                ReDim List(1 To 5) As String
                List(1) = "Mariners"
                List(2) = "Mets"
                List(3) = "Dodgers"
                List(4) = "Red Sox"
                List(5) = "Orioles"
                InitialSelected = -1
        End Select
    

    Note  In the ReDim statement in the preceding code, you must use the array range (1 to 5) when you redimension the List parameter. The List parameter is a 1-based array. However, if you don't specify an array range that starts the array at 1 and goes to whatever number is specified in the Count parameter, or if you specify a single array number without using an array range, your list box or combo box may not be displayed properly. The preceding code shows the recommended method for dimensioning the List parameter.

    The InitialSelected parameter specifies the number of the item to select when the radio group is initially displayed. The number of the item is the array index number as it is specified in the List parameter. Use -1 to indicate that none of the items in the list are initially selected.

  5. In the OnListOrComboSelectChange method, you would specify what happens when a user selects an item from the list. The following code displays a message box that contains the value of the item the user selected.

        Dim strText As String
    
        Select Case ControlID
            Case 601
                strText = "My favorite baseball team is " & value & "."
                MsgBox strText
        End Select
    
  6. Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.

Next  Adding Images