HOWTO: Gain Access to Items in the ListBox Object

ID: Q147846


The information in this article applies to:


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



  2. Removing items from lists



  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 query words:


Keywords          : kbcode kbOOP kbVFp300 kbVFp500 kbVFp600 
Version           : 
Platform          : 
Issue type        : kbhowto 

Last Reviewed: August 3, 1999