Share via


Adding Radio Buttons (Visual Basic 6.0 Tutorial) [Office 2003 SDK Documentation]

Previous  Adding Help Content

The following steps show you how to add a collection of radio buttons to the SimpleSample smart document.

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

    Const cRADIO As String = cNAMESPACE & "#radiobutton"
    
  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 = 5
    
  3. Now you are ready to modify the existing code. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.

    In the SmartDocXMLTypeName property subroutine, insert the following code.

            Case 5
                ISmartDocument_SmartDocXmlTypeName = cRADIO
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

            Case 5
                ISmartDocument_SmartDocXmlTypeCaption = "Radio buttons"
    

    In the ControlCount property subroutine, insert the following code.

            Case cRADIO
                ISmartDocument_ControlCount = 1
    

    In the ControlID property subroutine, insert the following code.

            Case cRADIO
                ISmartDocument_ControlID = ControlIndex + 400
    

    In the ControlTypeFromID property subroutine, insert the following code.

            Case 401
                ISmartDocument_ControlTypeFromID = C_TYPE_RADIOGROUP
    

    In the ControlCaptionFromID property subroutine, insert the following code.

            Case 401           
                ISmartDocument_ControlCaptionFromID = "Pick your favorite color"
    
  4. Next, you need to specify the value of the items in the radio buttons. To do this, you use the PopulateRadioGroup method.  Use the List array parameter to specify each item in the radio group, and use the Count parameter to specify the number of items in the List array parameter. Insert the following code into the PopulateRadioGroup method subroutine.

        Select Case ControlID
            Case 401
                Count = 5
                ReDim List(1 To Count) As String
                List(1) = "Red"
                List(2) = "Blue"
                List(3) = "Yellow"
                List(4) = "Purple"
                List(5) = "Green"
                InitialSelected = -1
        End Select
    

    Note  In the ReDim statement in the code above, it is necessary to use the array range (1 to 5) when redimensioning the List array parameter. The List array 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 radio buttons may not be displayed properly. The above code shows the recommended method for dimensioning the List array parameter.

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

  5. Then, in the OnRadioGroupSelectChange method, write the code that you want to run when a user selects an item from the list of radio buttons. The Target parameter can be used to access the element to which the control applies. In the following code, you first use the Target parameter to determine the application in which the smart document is displayed. Then you create a Microsoft Word Range or a Microsoft Excel Range object from the XML element, and replace the existing text in the XML element within the smart document with the new text specified in the String.

    Note  In order for this code to work, you must add a reference to the Microsoft Word 11.0 Object Library.

        Dim strText As String
        Dim objWdRange As Word.Range
    
        strText = "My favorite color is " & value & "."
    
        Select Case ControlID
            Case 401
                If Target.Application.Name = "Microsoft Word" Then
                    Set objWdRange = Target
                    objWdRange.XMLNodes(1).Text = strText
                    Set objWdRange = Nothing
                Else
                    MsgBox strText
                End If
        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 Check Boxes