Multiple Selection Feature of the ListBox Control


Visual Basic 6 allows you to use the multiple selection feature of the ListBox control.

I have shown you many properties and methods of the listbox control in the previous lesson. Many good features were explained. But the multiple selection is probably the nicest feature among all. It lets you work with the list box control even more flexibly.



How to enable the multiple selection?

Set the multi-select property to either 1-Simple or to 2-Extended to enable it. The values of multi-Select property are 0-None, 1-Simple and 2-Extended.

In the 1-Simple value of the Mult-Select property, you can select individual items at the same time using the mouse or by the space bar. But the 2-Extended value gives you an extra benefit by allowing the users to select the individual items by using the Shift key also. Generally speaking, the 2-Extended value is preferred by the majority programmers.


SelCount property

The SelCount property returns the number of selected items.



Selected property

The selected property takes the index, it returns True if the item is selected, returns False otherwise.



Multiple selection listbox demo:



Download this sample

Code:
Private Sub cmdClearAll_Click()
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = False
    Next
End Sub
___________________________________________
Private Sub cmdDelete_Click()
    For i = 0 To List1.SelCount - 1
        List1.RemoveItem List1.ListIndex
    Next
End Sub
___________________________________________
Private Sub cmdDeleteAll_Click()
    List1.Clear
End Sub
___________________________________________
Private Sub cmdSelectAll_Click()
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = True
    Next
End Sub
___________________________________________
Private Sub Form_Load()
    Form1.Show
  
    For i = 0 To 10
        List1.AddItem "Item" & i
    Next
End Sub



CheckBox-Style ListBox

Set the Style property to 1-CheckBox for a checkbox style listbox control.