Different Ways to Gain Access to Items in the ListBox Object

Last reviewed: October 17, 1996
Article ID: Q147846
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b, 5.0

SUMMARY

You can gain access to an item in a list box or combo box by referencing its position in the list (index) or its unique identification number (itemid). This article describes how the index method differs from the itemid method and gives the properties and methods related to each method.

MORE INFORMATION

Although all of this information also applies to lists with multiple columns, the examples in this article use one-dimensional lists only.

Following are some general and property definitions. For a more complete definition of these properties, please see the Help menu.

General       Definition
Item          an element of a list.
Index         a unique number assigned by you or by the system to an item.
Itemid        a unique number used to reference an item in a list.

Property      Definition
List          an array used to gain access to items in index order.
ListItem      an array used to gain access to items in itemid order.
ListIndex     the index of the selected item in a list.
ListItemId    the itemid of the selected item in a list.
ListCount     the number of items in a list.
NewIndex      the index of the item most recently added to the list.
NewItemId     the itemid of the item most recently added to the list.
Sorted        whether or not the items in a list are sorted alphabetically.
MultiSelect   whether or not a user can select multiple items.
MoverBars     allows movement of items in the list.
ItemData      uses an index to reference an internal array to store
              auxiliary data for an item.
ItemIdData    uses an itemid to reference an internal array to store
              auxiliary data for an item.

Methods

The following methods receive either the value of the index or the itemid. For a method that receives the index as a parameter, there is always an equivalent method that receives the itemid as a parameter. There is, however, no method that can receive both the index and the itemid. Note that all of the following methods work reliably only when the RowSource property is set to 0-None.

  1. Adding items to lists

        - AddItem adds items to a list and receives as an optional parameter a
          valid index for the new item. The index is useful only when the list
          is not sorted (Sorted = .f.), which is the default. You can't specify
          an index larger than ListCount.
    

          For example, if you have a list called List1 on a form, you can add
          items by using this code:
    

             thisform.List1.AddItem('A') adds 'A' to the list
             thisform.List1.AddItem('B',3) adds 'B' to the list at index 3
    
          The resulting list would be as follows.
    
                 Item      Index   ItemId
                   A         1       1
                   Z         2       2
                   C         3       3
    
        - AddListItem adds items to a list and receives a valid itemid as an
          optional parameter. The itemid can range from 1 to 32767. For
          example, use the following code to add 'a' to the list:
    
             thisform.List1.AddListItem('a')
    
          Use the following code to add 'b' to the list with an itemid of 14:
    
             thisform.List1.AddListItem('b',14)
    
          The resulting list would be as follows.
    
               Item      Index     ItemId
                 A        1          1
                 B        2         14
    
    

  2. Removing items from lists

        - RemoveItem deletes an item from a list by its index number. For
          example, use the following syntax to remove the second item in a
          list:
    

          thisform.List1.RemoveItem(2) removes the second item in a list.
    

        - RemoveListItem deletes an item from a list by its itemid number. For
          example, to remove the item with an itemid of 342, type this command:
    

          thisform.List1.RemoveListItem(342)
    

  3. Converting from Index to Itemid

    If you know the index of an item and need to know its corresponding itemid, you can call IndexToItemId to convert an index of an item to its corresponding itemid.

    If you know the itemid of an item and need to know its corresponding index, you can call ItemIdToIndex to convert an itemid of an item to its corresponding index.

  4. The ItemData and ItemIdData Array

    ItemData and ItemIdData both use the same array, but they use two different methods. This array is different from the list array and can store long integers (1 to 2,147,483,647). It can be used to store data that you do not want in a list, but need as a reference to link to the data. The array is also populated when the AddItem and AddListItem methods add items to a list. The array elements are initially zero and are not reinitialized when items are removed with the RemoveItem and RemoveListItem methods.

    For example, use the following syntax to assign a value to the itemdata array for the last element added to the list:

    thisform.List1.AddItem('b') thisform.List1.ItemData(thisform.List1.NewIndex) = 1253

  5. Multi-Select Lists

    By setting the MultiSelect property to true (.T.), you can manually select more than one item in the list. You can then use the Selected property of the list to determine which items in the list are selected.

    For example:

          FOR I=1 TO thisform.List1.ListCount
    
            IF thisform.List1.Selected(I)
                =MESSAGEBOX("Item: "+ALLTRIM(thisform.List1.List(I))+ ;
                " is selected.")
            ENDIF
          NEXT
    
       If the MultiSelect property is false, the ListIndex and the ListItemId
       properties return the selected item.
    


Additional reference words: 5.00 3.00 3.00b VFoxWin combobox
KBCategory: kbprg kbhowto kbcode
KBSubcategory: FxprgClassoop


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.