VB6 Tutorial 41: Listbox Multiselect

Posted On 2018-04-27 by VB6Boy
Keywords:
Tags: VB6 Tutorials 
Views: 67

Title Uploaded Size
vb41-Listbox_MultiSelect.zip 4/27/2018 9:43:41 AM 1,261


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

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. See the attached sample project called ListboxMultiSelect01 for a peek at multiselect in action.

Code

Below is the code for the example project. Note that in this example, I have used a private subroutine to reload the listbox.

The LoadListBox subroutine defines the variable MyList as an object, so the variable can be treated just like an any other object. In this example we can pass any listbox to the subroutine, and it will clear and re-load the specified listbox.

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 cmdReload_Click()
    LoadListbox List1
End Sub

Private Sub cmdSelectAll_Click()
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = True
    Next
End Sub

Private Sub Form_Load()
    LoadListbox List1
End Sub

Private Sub LoadListbox(MyList As ListBox)
    MyList.Clear
    For i = 0 To 10
        MyList.AddItem "Item" & i
    Next
End Sub

CheckBox-Style ListBox

For a multi-select checkbox, you also have the option of making it a checkbox style listbox, wherein the user would be able to click a box to select each item on the list.

In the sample project, try changing the Style Property to 1-Checkbox, and see how the program behaves.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

has posted a total of 78 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.166.212.152

Before you can post, you need to prove you are human. If you log in, this test goes away.


Code Links