See Also Example
Applies To
Combo box, directory list box
Description
Determines the items contained in a control's list portion. The list is a string array in which each element is a list item. Not available at design-time; read-only at run time for a directory list box, drive list box, and file list box; read/write at run time for a combo box and list box.
Usage
[form.]control.List(index)[ = stringexpression ]
Remarks
Use this property to access list items.
For all controls except the directory list box, the index of the first item is 0 and the index of the last item is ListCount - 1.
For a directory list box, the index number sequence is based on the directories and subdirectories present when the control is created at run time. The currently expanded directory is represented using the index -1. Directories above the currently expanded directory are represented by negative indexes with higher absolute values. For example, -2 is the parent directory of the currently expanded directory and -3 is the directory above that. Directories below the currently expanded directory range from 0 to ListCount -1.
Initially, combo boxes and list boxes contain an empty list. For the file-system controls, the list is based on conditions that exist when the control is created at run time:
Directory list boxcontains a list of all directories, using the range -n to ListCount - 1.
Drive list boxcontains the list of drive connections in effect.
File list boxcontains the list of files in the currently expanded directory that match the Pattern property. The path is not included.
The List property works in conjunction with the ListCount
For all applicable controls except a directory list box, enumerating a list from 0 to ListCount -1 returns all items in the list. For a directory list box, enumerating the list from -n to ListCount -1 returns a list containing all directories and subdirectories visible from the currently expanded directory. In this case n is the number of directory levels above the currently expanded directory.
Note To specify items you want to display in a combo box or list box, use the AddItem method. To remove items, use the RemoveItem method. To keep items in alphabetical order, set the control's Sorted property to True before adding items to the list.
Using an Option Base = 1 statement in the Declarations section does not affect the enumeration of elements in Visual Basic controls. The first element is always 0.
When the List index is outside the range of actual entries in the list box, an empty string is returned. For example, List(-1) returns an empty string for a combo box or list box.
Data Type
String array
Help:
ListCount Property
ListIndex Property
Programmer's Guide:
Chapter 3, "Creating and Using Controls"
The example loads a combo box with a list of sandwich names and displays the first item in the list. To try this example, paste the code into the Declarations section of a form that contains a combo box. Then press F5.
Sub Form_Load ()
Combo1.AddItem "Denver Sandwich" ' Add each item to list.
Combo1.AddItem "Reuben Sandwich"
Combo1.AddItem "Turkey Sandwich"
Combo1.Text = Combo1.List(0) ' Display first item.
End Sub