Share via


Adding Radio Buttons (Visual C++ 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.

    #define cRADIO(xmlns) xmlns L"#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.

    #define cTYPES 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:
                    *Name = SysAllocString(cRADIO(cNAMESPACE));
                    break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 5:
                    *Caption = SysAllocString(L"Radio buttons");
                    break;
    

    In the ControlCount property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cRADIO(cNAMESPACE)) == 0)
            {           
                *Count = 1;
            }
    

    In the ControlID property subroutine, insert the following code.

            else if (wcscmp(XMLTypeName,cRADIO(cNAMESPACE)) == 0)
            {           
                *ControlID = ControlIndex + 400;
            }
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 401:
                    *Type = C_TYPE_RADIOGROUP;
                    break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

                case 401:
                    *Caption = SysAllocString(L"Pick your favorite color");
                    break;
    
  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 parameter to specify each item in the radio group, and use the Count parameter to specify the number of items in the List parameter. Insert the following code into the PopulateRadioGroup method subroutine.

            switch (ControlID)
            {
                case 401:
    
                int cElements = 5;
                WCHAR* rgszRadios[] = {L"Red",
                                       L"Blue",
                                       L"Yellow",
                                       L"Purple",
                                       L"Green"};
    
                for (long i = 1; i <= cElements; i++)
                {
                    SafeArrayPutElement(*List, &i, SysAllocString(rgszRadios[i-1]));
                }
    
                *Count = cElements;                    
                *InitialSelected = m_lRadioSelection;            
    
                break;            
    
            }
    

    Note  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.

    For this code to compile without error, you need to insert the following global variable.

    Variable Type Variable Name
    long m_lRadioSelection

    How?

    • Right-click the CActions class in the Class View.
    • Click Add Member Variable.
    • Type long in the Variable Type text box.
    • Type m_lRadioSelection in the Variable Name text box.
    • Click OK.
  5. In the OnRadioGroupSelectChange method, write the code that you want to run when a user selects an item from the list of radio buttons. You can use the Target parameter to access the element to which the control applies. In the following code, you create a Microsoft Word Range object from the XML element and replace the existing text in the Word document with the new text specified in the variable.

            switch (ControlID)
            {
                case 401:
    
                    CString szText("My favorite color is ");
                    szText+=Value;
    
                    SetXMLNodeText(Target,1,szText,false);
    
                    m_lRadioSelection = Selected;
    
                    break;
            }
    
  6. Finally, you create the custom function called SetXMLNodeText that you called in the previous code. This function sets the text for the current XML node. To do this, add the following code to the actions.h file before the #endif line.

    extern void SetXMLNodeText(IDispatch* Target,int nIndex, LPCTSTR szText,bool bAppend);
    

    Then insert the following code at the end of the CActions.cpp file.

        void SetXMLNodeText(IDispatch* Target, int nIndex, LPCTSTR szText, bool bAppend = false)
        {
    
            Range objRange,objNodeRange;        
            XMLNodes objXMLNodes;    
            XMLNode objXMLNode;            
    
            objRange.AttachDispatch(Target);
            objXMLNodes.AttachDispatch(objRange.GetXMLNodes());
            objXMLNode.AttachDispatch(objXMLNodes.Item(nIndex));
            objNodeRange.AttachDispatch(objXMLNode.GetRange());
    
            CString strText=objNodeRange.GetText();
            if (bAppend)
                strText+=szText;
            else
                strText=szText;
    
            objNodeRange.SetText(strText);
    
            objNodeRange.DetachDispatch();
            objXMLNode.DetachDispatch();
            objXMLNodes.DetachDispatch();
            objRange.DetachDispatch();        
        }
    
  7. Recompile your SimpleSample smart document dynamic-link library (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