Adding List Boxes and Combo Boxes (Visual 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.

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

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

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 7:
                    *Caption = SysAllocString(L"List box");
                    break;
    

    In the ControlCount property subroutine, insert the following code.

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

    In the ControlID property subroutine, insert the following code.

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

    In the ControlTypeFromID property subroutine, insert the following code.

                case 601:
                    *Type = C_TYPE_LISTBOX;
                    break;
    

    In the ControlCaptionFromID property subroutine, insert the following code.

                case 601:
                    *Caption = SysAllocString(L"Select your favorite fruit.");
                    break;
    
  4. The PopulateListOrComboContent method specifies the value of the items in the list. Lists are created in a manner similar to that of 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:
    
                int cElements = 5;
                WCHAR* rgszList[] = {L"Apples",
                                      L"Oranges",
                                      L"Bananas",
                                      L"Pears",
                                      L"Peaches"};
    
    
               for (long i = 1; i <= cElements; i++)
                {
                    SafeArrayPutElement(*List, &i, SysAllocString(rgszList[i-1]));
                }
    
                *Count = cElements;                    
                *InitialSelected = m_lListSelection;            
    
                break;            
    
            }
    

    Note  The InitialSelected parameter specifies the number of the item to select when the radio button 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.

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

    Variable Type Variable Name
    long m_lListSelection

    How?

    • Right-click the CActions class in the Class View.
    • Click Add Member Variable.
    • Type long in the Variable Type text box.
    • Type m_lListSelection in the Variable Name text box.
    • Click OK.
  5. In the OnListOrComboSelectChange method, you 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.

            switch (ControlID)
            {
            case 601:    
                CString szText("My favorite fruit is ");
                szText+=Value;
    
                SetXMLNodeText(Target,1,szText,false);
    
                m_lListSelection = Selected;
                break;
            }
    

    Note  If you don't have the SetXMLNodeText function, see Adding Radio Buttons.

  6. 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 Images