Lists and Combo Boxes


    Both list box controls and combo box controls allow you to have a list of items from which the user can make a selection.

    List boxes and combo boxes have most of the same properties and operate in a similar fashion. One exception is that a combo box have a Text property and list boxes do not. Both list and combo boxes allow you to list more items than can be seen at one time, given the size of the box, VB automatically adds a scroll bar. When you name list boxes and combo boxes, use "lst" as the list box prefix and "cbo" as the prefix for combo boxes.

Filling the List

Using the Properties Window

    If you know the list contents at design time and the list never changes, you can define the list items in the Properties window. When entering your list items, use CTRL + ENTER to move to the next list item.

Using the AddItem Method

    To add an item to a list at run time, use the AddItem method. You may choose o add to a list the contents of the text box at the top of a combo box, a variable, a constant, or the property of another control.

Object.AddItem Value [, Index]

    Value is the value to add to the list. If the value is a string literal, enclose it in quotation marks. The optional Index specifies the position within the list to place the new item; the first element in the list has in Index of 0. When you omit the Index, the new item generally goes at the end of the list. However, you can alter that by setting the control's Sorted property to True. Then the new item will be placed alphabetically in the list.

Examples,

lstSchools.AddItem "Harvard"
lstSchools.AddItem "Stanford"
lstSchools.AddItem txtSchools.Text
lstSchools.AddItem cboMajors.Text
lstSchools.AddItem stMajor

    When the user types a new value in the text box portion of a combo box, that item is not automatically added to the list. If you want to add the newly entered text to the list, use the AddItem method:

cboCoffee.AddItem   cboCoffee.Text

Clearing the List

    Use the Clear method to empty a combo box or list box.

Object.Clear

Examples,

lstSchools.Clear
cboMajors.Clear

The ListCount Property

The ListCount property stores the number of items in the list. You can use it to process each element in the list. It is also handy when you need to display the count at some point in your project (HINT: ListCount is always one more than the number of items in your list).

Example,    iTotalItems = 1stItem.ListCount

The List Property

    If you need to display one item from the list, you can refer to one elemtn of the List property. The index of the first list element is 0, so the highest index is ListCount negative 1.

Object.List(Index)     [  = Value ]

Examples,

lstSchools.List(5) = "University of California"
lblMyMajor.Caption = cboMajors.List (iIndex)
lblSelectedMajor.Caption = cboMajors.List(cboMajors.ListIndex)

Removing an Item from a List

    To remove an element from the list, use the RemoveItem Method. The Index is required as it specifies which element to remove.

Object.RemoveItem    Index

Examples,

1stNames.RemoveItem 0     ' Remove the first name from the list
cboSchools.RemoveItem  iIndex    ' Remove the element in position iIndex
cboCoffee.RemoveItem  cboCoffee.ListIndex  'Remove the currently selected item