Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

The List Box Control

Figure 10.1 shows the Editor Format tab of the Options dialog box, which appears when you select Tools | Options and click the Editor Format tab. The dialog box illustrates a List Box control. You've seen list boxes throughout your work with Windows; list boxes appear on many forms and dialog boxes. The List Box control gives the user a choice of several values. The user selects an option instead of typing a value into a text box. The list box ensures that the user always chooses one of the available options.


Figure 10.1 The List Box control gives the user a choice of options.

As you place the list box on the form, think about the data the list box will hold and try to size the list box so that it's large enough to hold the data. Of course, you don't always now a list box's data in advance because the data might come from a disk file or from the user at the keyboard. Your form's size and surrounding controls might limit the size of your list box, so the scrollbars often appear.

Any list box can have a single or multiple columns. In many situations the single column makes data selection easier for your users, but they will have to scroll through more values to locate the item they want to find. Figure 10.2 shows a form with two list boxes; the first list box is a single-column list box, and the second displays three columns. (The Columns property determines the list box's number of columns.)


Figure 10.2 A list box with one column and one with three columns.

To familiarize yourself with list boxes as quickly as possible, look over the property values in Table 10.1. You'll work with other properties at runtime because you often initialize the list box at runtime and not at design time.

Table 10.1. The basic list box properties.

Property Description
BackColor Specifies the list box's background color.
Columns Determines the number of columns. If 0, the list box scrolls vertically in a single column. If 1 or more, the list box items appear in the number of columns specified (one or more columns), and a horizontal scrollbar appears so that you can see all the items in the list.
ForeColor Specifies the list box's text color.
Height Indicates the height of the list box in twips.
IntegralHeight Determines whether the list box can display partial items, such as the upper half of an item that falls toward the bottom of the list box.
List Holds, in a drop-down property list box, values that you can enter into the list box at design time. You can enter only one at a time, and most programmers usually prefer to initialize the list box at runtime.
MultiSelect The state of the list box's selection rules. If 0-None (the default), the user can select only one item by clicking with the mouse or by pressing the Spacebar over an item. If 1-Simple, the user can select more than one item by clicking with the mouse or by pressing the Spacebar over items in the list. If 2-Extended, the user can select multiple items using Shift+click and Shift+arrow to extend the selection from a previously selected item to the current one. Ctrl+click either selects or deselects an item from the list.
Sorted Determines whether the list box values are automatically sorted. If False (the default value), the values appear in the same order in which the program added the items to the list.
Style Determines whether the list box appears in its usual list format or, as shown in Figure 10.3, with check boxes next to the selected items.

Figure 10.3 You can add check boxes to list box items.

Table 10.2 describes the methods available to the list box. Remember that methods are routines a control knows how to execute. List boxes use methods more than any other control you've learned about so far. The methods help the user initialize, add items to, and remove items from list boxes.

Table 10.2. Common list box methods.

Method Description
AddItem Adds a single item to the list box.
Clear Removes all items from the list box.
List A string array that holds items from within the list box.
ListCount The total number of list box items.
RemoveItem Removes a single item from the list box.

Perhaps the most important method is the AddItem method, which adds items to the list box. AddItem is to list boxes what the assignment statement is to variables. A method always appears between the control name and a period. For example, the following AddItem method sends the value of Joseph to a list box named lstOneCol:

lstOneCol.AddItem "Joseph"

You'll often initialize a list box in the Form_Load() event procedure that initializes the form and the form controls right before the form appears on the screen. The following code sends several people's names to the list boxes shown earlier:

lstOneCol.AddItem "Joseph"
lstOneCol.AddItem "Angel Sue Bush"
lstOneCol.AddItem "Michael"
lstOneCol.AddItem "Stephanie"
lstOneCol.AddItem "Mary Ann"
lstOneCol.AddItem "Pamela"
lstOneCol.AddItem "Jock"
lstOneCol.AddItem "Bobby"
lstOneCol.AddItem "Cliff"
lstOneCol.AddItem "Jerry"
lstOneCol.AddItem "Thomas"
lstOneCol.AddItem "George"
lstOneCol.AddItem "Robert"

Each item in a list box contains an associated subscript. The subscript is a number that begins at 0 for the first item, the second subscript is 1, and so on. Therefore, if you apply the RemoveItem method as follows, the third item is removed (because of the first item's 0 subscript):

lstOneCol.RemoveItem(2)   ' 3rd item has a subscript of 2

A subscript is a value that distinguishes one array item from the other array items.

As you remove list box items, the remaining item subscripts adjust upward accordingly. Therefore, if a list box contains seven items, each item has a subscript that ranges from 0 to 6. If you remove the fourth item, the list box items will then range from 0 to 5; the subscript 5 will now indicate the same item that the subscript 6 indicated before RemoveItem removed the fourth item.

You can remove all items from the list box with Clear, like this:

lstOneCol.Clear     ' Remove all items

You can assign individual items from a list box that contains data using the List method. You must save list box values in String or Variant variables unless first you convert the items to a numeric datatype using Val(). The following statements store the first and fourth list box items in two String variables:

strVar1 = lstOneCol.List(0)
strVar2 = lstOneCol.List(3)

The List method requires a subscript so Visual Basic knows which value from the list to assign to the variable. The value remains in the list after the assignment, but now the value appears in the variable as well.

You use ListCount to determine the number of items in the list box currently defined. The following statement stores the number of list box items in a numeric variable named intNum:

intNum = lstOneCol.ListCount

You use Selected to determine whether a user has selected a list box item. Selected returns True for one or more list box items if the item's MultiSelect property is set to either 1-Simple or 2-Extended. Those properties indicate that the user can select more than one item at once. Figure 10.4 shows a list box with several items selected at the same time.


Figure 10.4 A list box with a MultiSelect property set to 1 or 2.

+ Share This