As you know, list boxes display a list of items from which the user can select one or more. If there are too many items to display at once, a scroll bar automatically appears to let the user scroll through the list. In Visual Basic .NET, each item in a list box is itself an object. You can see a list box—this is the ListBoxes example on the CD-ROM—in Figure 7.1.
You also can scroll list boxes horizontally when you set the MultiColumn property to True. Alternatively, when the ScrollAlwaysVisible property is set to True, a scroll bar always appears.
How do you find out which item is selected in a list box? You can keep track of the selected item in a list box two ways—by numeric index (starting at 0) or by accessing the selected item's object directly. The SelectedIndex property returns an integer value that corresponds to the selected item. If the first item in the list is selected, then the SelectedIndex value is 0. You can change the selected item by changing the SelectedIndex value in code; the corresponding item in the list will appear highlighted on the Windows form. If no item is selected, the SelectedIndex value is -1. You also can set which items are selected with the SetSelected method in code.
The SelectedItem property is similar to SelectedIndex, but returns the object corresponding to the item itself (which is usually a string value, but need not be—see "Storing Objects in a List Box or Combo Box" later in this chapter).
The items in list boxes are stored in the Items collection; the Items.Count property holds the number of items in the list. (The value of the Items.Count property is always one more than the largest possible SelectedIndex value because SelectedIndex is zero-based.) To add or delete items in a ListBox control, you can use the Items.Add, Items.Insert, Items.Clear, or Items.Remove methods. You also can add a number of objects to a list box at once with the AddRange method. Or you can add and remove items to the list by using the Items property at design time.
You also can use the BeginUpdate and EndUpdate methods. These enable you to add a large number of items to the ListBox without the list box being redrawn each time an item is added to the list. The FindString and FindStringExact methods enable you to search for an item in the list that contains a specific search string.
You also can support multiple selections in list boxes. The SelectionMode property determines how many list items can be selected at a time; you can set this property to None, One, MultiSelect, or MultiExtended:
MultiExtended— Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections.
MultiSimple— Multiple items can be selected.
None— No items may be selected.
One— Only one item can be selected.
When you support multiple selections, you use the Items property to access the items in the list box, the SelectedItems property to access the selected items, and the SelectedIndices property to access the selected indices.
And in addition to all this, we'll also see how to handle list box events in this chapter.