Share via


Adding List Boxes and Combo Boxes (C# 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 would 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 String cLIST = 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 Int32 cTYPES = 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:
                   strTypeName = cLIST;
                   break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 7:
                   strTypeCaption = "List box";
                   break;
    

    In the ControlCount property subroutine, insert the following code.

                case cLIST:
                   intNumberOfControls = 1;
                   break;
    

    In the ControlID property subroutine, insert the following code.

                case cLIST:
                   intControlID = ControlIndex + 600;
                   break;
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 601:
                   type = C_TYPE.C_TYPE_LISTBOX;
                   break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

                case 601:
                   strControlCaption =  "Select your favorite baseball team.";
                   break;
    
  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; then the List parameter specifies the value of each of the five items in the list.

             switch (ControlID){
                case 601:
                   Count = 5;
                   List.SetValue("Mariners", 1);
                   List.SetValue("Mets", 2);
                   List.SetValue("Dodgers", 3);
                   List.SetValue("Red Sox", 4);
                   List.SetValue("Orioles", 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. 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 specifies that new text that contains the value of the selected item replaces the current text in the active element.

             Microsoft.Office.Interop.Word.Range objRange;
      
             switch (ControlID)
             {
                case 601:
                   objRange = (Microsoft.Office.Interop.Word.Range)Target;
                   objRange.XMLNodes[1].Range.Text = "My favorite baseball team is " + 
                      Value + ".";
                   break;
    
                default:
                   break;
             }
    
  6. Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier.

  7. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.

Next  Adding Images