List and Combo box controls


Both List and Combo boxes share most of the same methods and properties. The most significant difference between the two is their presentation on the screen: a list box is a list of items that is always displayed in a bounding box the size of the list; the combo box is a drop-down style list that is displayed only when the user clicks on the down arrow, thus saving screen space for long lists.

ListBox Control

A ListBox control displays a list of Items and a user can select or de-select each item in the list. The Items property can be manipulated at design time and also in your code during runtime.
If the number of items exceeds the amount that can be displayed, a scroll bar is automatically added to the ListBox control.

ComboBox Control

A ComboBox control combines the features of a TextBox control and a ListBox control—users can enter information in the text box portion and/or select an item from the list box portion of the control.

Accessing and Manipulating List Items

The following code samples assume a listbox with the name "lstAnimals" and it contains at least 4 items in the list.

' returns the item number in list or combobox that is selected
intItemNum = lstAnimals.SelectedIndex

' returns the text value of the item that is selected
strAnimal = lstAnimals.SelectedItem

' returns the total number of items in the list
intTotalNum = lstAnimals.Items.Count

' adds an item to the end of the list
lstAnimals.Items.Add("Zebra")

' removes an item named "Zebra" from the list
lstAnimals.Items.Remove("Zebra")

' removes an item at the position number 3
lstAnimals.Items.RemoveAt(3)

' removes all items from the list
lstAnimals.Items.Clear()

 

 

 

 

 

Note: If no item is selected, the SelectedIndex property value is -1. The first item in the list is SelectedIndex 0, and the value of the Items. Count property is aways one more than the largest SelectedIndex value.