Share via


Visual Basic Concepts

List Box Control Scenario 2: Creating Multiple-Column List Boxes

To create a multiple-column, multiple-selection list box, you need to set both the Columns and the MultiSelect properties of a list box. In the following example, these properties are used to create such a list box.

You'll notice that when you run the application, the list box contains two columns, as shown in Figure 7.37.

Figure 7.37   Multiple-column list box

If you draw the list box large enough to hold all the items in one column, the second column will be empty; the other items will wrap, and horizontal scroll bars will appear automatically only if the list box is not long enough. Try resizing the top list box and adding additional list items to see how Visual Basic automatically handles multiple columns.

The example uses the Selected property — a Boolean array containing the selection status of a list box — to determine which items are selected. Each entry in the array corresponds to a list item and is set to True if the item is selected, or False if it is not selected. After the user selects items from the list, each array entry is checked to see if it is set (True). If so, the entry is added to the second list, a normal single-column list box, using the AddItem method.

Set the properties for the example as indicated in the following table.

Object Property Setting
Form Caption Multiple-Column List Box
Top list box Name
Columns
MultiSelect
lstTop
2
2-Extended
Bottom list box Name lstBottom
First command button Name
Caption
cmdTransfer
&Transfer
Second command button Name
Caption
cmdClear
C&lear
Third command button Name
Caption
cmdClose
&Close

The MultiSelect property allows you to select a range of values in a list box. If you click the first list item, and then press SHIFT and click the last item in the range (or use the SHIFT+ DOWN ARROW keys), all the items in the range are selected.

Events in the Multiple-Column List Box Application

Add code to the Form_Load procedure to initialize the top list, 1stTop:

Private Sub Form_Load ()
   lstTop.AddItem "Paris"
   lstTop.AddItem "New Orleans"
   lstTop.AddItem "San Francisco"
   lstTop.AddItem "Chicago"
   lstTop.AddItem "Seattle"
   lstTop.AddItem "Toronto"
   lstTop.AddItem "New York"
   lstTop.AddItem "Tbilisi"
   lstTop.AddItem "Moscow"
   lstTop.AddItem "Portland"
   ' Select a couple of items.
   1stTop.Selected(0) = True
   1stTop.Selected(1) = True
End Sub

Note   You can add items to list boxes without repeatedly using the AddItem method by typing items in the List property of the Properties window. After entering each item, press CTRL+ENTER to go to the next line. This allows you to type multiple entries in a multiple-column list box.

Add the following code to the 1stTop_DblClick event procedure:

Private Sub 1stTop_DblClick ()
   cmdTransfer.Value = True   ' Press transfer button.
End Sub

Add the following code to the Click event procedure for the Transfer command button:

Private Sub cmdTransfer_Click ()
   For n = 0 To (lstTop.ListCount - 1)
' If selected, add to list.
      If lstTop.Selected(n) = True Then
         lstBottom.AddItem lstTop.List(n)
      End If
   Next
   cmdClear.Enabled = True
End Sub

Notice how the array index values start from 0 and go to ListCount -1.

Add the following code to the Click event procedure for the Clear command button:

Private Sub cmdClear_Click ()
   lstBottom.Clear
   cmdClear.Enabled = False
End Sub

Add the following code to the Click event procedure for the Close command button.

Private Sub cmdClose_Click ()
   Unload Me
End Sub