Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.

The List Box

The list box is as it sounds—a box that contains a list, such as department names in the college of business, names of students in a VB class, or fixed assets a company owns. When the list of elements in the box is longer than the physical height of the list box, the box shows a vertical scrollbar that the user can use to scroll up and down to browse its content. The item that the user clicks is identified as the list’s SelectedItem property. The list box’s SelectedIndex property, also set in the Click event, indicates the position of the item clicked. For example, suppose the list box in Figure 3-12 is named lstMonths. If the user clicks March in the list, lstMonths.SelectedItem will show March, and lstMonths.SelectedIndex will give a value of 2. Note that the first position in the list has a SelectedIndex value of 0, not 1. You can also use code to select an item. For example, the following code will select the first item in the list and thus set the SelectedItem property to January.
LstMonths.SelectedIndex = 0

Figure 3-12
List box: icon and appearance

Populating the List Box

How do you build a list for the list box? If you have a short list with known data that will not change, you can create the list at design time. To do so, follow these steps:

  1. Click the list box in the form.
  2. Click the Items property in the Properties window.
  3. Click the ellipsis (…) button in the property box. The String Collection Editor will appear (Figure 3-13).
  4.  Key your list into the box. At the end of each list item, press Enter to proceed to the next item.
  5. Click the OK button when the list is complete.

If you have a fairly long list or the data items are not known at design time, you will need to build the list by code using the Items.Add method. This method has the following syntax:


For example, the following code will add John Wayne to the list box named lstMyFriends.

lstMyFriends.Items.Add( “John Wayne”)

The Items.RemoveAt Method

You can also use the code to remove any item from the list box using the Items.RemoveAt method. For example, the following code will remove the third item from the list.
Note that when you remove an item from the list, all subsequent indices are automatically adjusted. In this example, the previous fourth item will now have an index value of 2 because it is now the third item.

Figure 3-13
The Items property and the String Collection Editor

The Items.Count Property

The list box has an Items.Count property that gives the number of items in the list box. At any time, its value represents the actual count. Because the list’s SelectedIndex starts at 0, the highest SelectedIndex value is Items.Count – 1. The Items.Count property of lstMonths as shown previously in this section will give a value 12, and its highest SelectedIndex value should be 11.

The SelectionMode Property

The list box has an interesting property, the SelectionMode property. It can have four alternative settings: None, One, MultiSimple, or MultiExtended. The default setting is One, meaning only one item can be selected from the list box. If the setting is None, nothing can be selected from the list. The user cannot click and select any item at runtime. If the property is set to MultiSimple or MutiExtended, the list box will allow multiple selections from its content. The difference between these two settings lies in the way the list box responds to the mouse click and combinations of keys.

Look It Up
Use the keywords SelectionMode enumeration to search the Index tab for explanation of how the list box behaves under different SelectionMode settings.

The Sorted Property

The Sorted property of the list box can be set to False (default) or True at design time or runtime. When the property is set to False, items in the box will appear in the order as originally set. If the property is set to True, items will appear in ascending order regardless of when the item is added to the list. This can be convenient for the user when searching for an item.

Versatility of the List Box

The versatility of the list box is impressive, but how does this relate to data entry? The list box was introduced as an alternative to radio buttons and check boxes when there are too many known items to show onscreen. The list box has the capability to hold many items, and it requires only limited space because its vertical scrollbar can be used to browse various parts of its contents. When its SelectionMode property is set to One, it is a good alternative to radio buttons. When its SelectionMode property is set to MultiSimple or MultiExtended, it becomes a good alternative to check boxes. In addition, the list box has the flexibility to add items to the list either at design time or at run time without the need to adjust its visual appearance.

The SelectedItem and SelectedIndex Property

How does your program know which item in the list box is selected? As explained previously in this section, when the user clicks an item, the list box sets its Text and SelectedIndex properties. When the SelectionMode property is set to One, the item clicked is the item selected and highlighted. Your program can then use either the SelectedItem property or the SelectedIndex property to identify what is selected.

Identifying the Selected Items

When the list box is set to accept multiple selections (that is, when the SelectionMode property is set to either MultiSimple or MultiExtended), the list box behaves similarly to the check box. The user can select or deselect an item by clicking it. The SelectedItem and the SelectionIndex properties give the first item selected and nothing more. To get the selected items, use the SelectedIndices and SelectedItems properties, instead. SelectedIndices(0) gives the index value of the first selected item, whereas SelectedItems(0) gives the value of the first selected item. Both properties have a Count property that gives the number of items selected. Suppose you have a list box named lstMonths for the user to select the months of interest. The following code should show all the months selected by the user:

For I = 0 to lstMonths.SelectedItems.Count – 1 MsgBox(lstMonths.SelectedItems(I) Next

Notice that the index begins with 0, thus SelectedItems(0) refers to the first element in the list of selected items. Note also that the parameter (the value in the pair of parentheses) does not have to be a constant. It can also be a variable—a name that represents some value. Recall Chapter 2 examined a code structure for the For loop:

For Counter = 1 to 10 Statements to be executed Next Counter

Statements inside the structure will be executed 10 times. The first time, Counter will have a value 1; second time, 2; and so on until Counter is greater than 10. In the case of selected items in the list box, the first item in SelectedItems has an index of 0 and the last item has an index of SelectedItems.Count – 1.

To determine whether an item is selected, you can use the GetSelected method. This method has the following syntax:


This method checks if the item in the index is selected. If so, it returns a value of True; otherwise, False. For example, the following code shows how you can decide whether the first item of a list box named lstMajors is selected.

If lstMajors.GetSelected(0) Then MsgBox(“The first item is selected.”) Else MsgBox(“The first item is not selected.”) End If

If you are interested in inspecting whether each item in the list is selected, you can code the following:

For I = 0 to lstMajors.Items.Count – 1 If lstMajors.GetSelected(I) Then MsgBox(“Item no. “ & I & “ is selected.”) Else MsgBox(“Item no. “ & I & “ is not selected.”) End If Next I

Again, as mentioned in Chapter 2, the & operator concatenates two strings into one. If the first item is selected, the message box will display the following:

Item no. 0 is selected.

Chapter 7, “Repetition,” discusses loops in more detail.

Use the SelectedIndices property to identify the positions of those items corresponding to the SelectedItems property.

The Checked List Box, a Variant

VB 2008 also has a control recognized as the checked list box. This control has the same properties, which behave almost exactly the same as the list box except for its appearance. While the standard list box displays each item as plain text, the checked list box displays a check box in front of each item, making it look like a check box (Figure 3-14). This control has two additional properties, CheckedIndices and CheckedItems, to allow your program to identify which items have been checked. You can use these properties in the same way as the SelectedIndices and SelectedItems properties in the standard list box.  Note that this control allows for only one selection even when multiple items are checked. (It does not support either of the multi-selection settings of the SelectionMode property.) The selected item is highlighted and can be different from the checked items. In most of your programs, you should find CheckedIndices and CheckedItems properties more pertinent when using the checked list box.

Figure 3-14

Last change: February 13 2016 18:47:28.