Share via


Adding Radio Buttons (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 will 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 String cRADIO = 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 Int32 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:
                  strTypeName = cRADIO;
                  break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

               case 5:
                  strTypeCaption = "Radio buttons";
                  break;
    

    In the ControlCount property subroutine, insert the following code.

               case cRADIO:
                  intNumberOfControls = 1;
                  break;
    

    In the ControlID property subroutine, insert the following code.

               case cRADIO:
                 intControlID = ControlIndex + 400;
                 break;
    

    In the ControlTypeFromID property subroutine, insert the following code.

               case 401:
                  type = C_TYPE.C_TYPE_RADIOGROUP;
                  break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

               case 401:        
                  strControlCaption = "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:
                     Count = 5;
                     List.SetValue("Red",1);
                     List.SetValue("Blue",2);
                     List.SetValue("Yellow",3);
                     List.SetValue("Purple",4);
                     List.SetValue("Green",5);
                     InitialSelected = -1;
                     break;
    
                  default:
                     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.

  5. Then, in the OnRadioGroupSelectChange method, write the code that you want 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 create a Word Range object from the XML element and replace the existing text in the Word document with the new text specified in the String.

               Microsoft.Office.Interop.Word.Range objRange;
      
               switch (ControlID){
                  case 401:
                     objRange = (Microsoft.Office.Interop.Word.Range)Target;
                     objRange.XMLNodes[1].Range.Text = "My favorite color is " + 
                        Value + ".";
                     break;
    
                  default:
                     break;
               }
    
  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