Multi Select List Box

51,311

Solution 1

Did you set the selectionmode to multi?

You need to specify that in order to allow multiple selections.

Then you can do:

Dim i as Integer=0

For i=0 To Me.listBox.SelectedItems.Count -1
  'display the listbox value
next i

Here is a screen shot:

enter image description here

After you set the property on the listbox then call setselected based on the values you want selected.

me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)

Here you can add 20 numbers and only select the even.

    Dim i As Integer

            'load the list with 20 numbers
            For i = 0 To 20
                Me.ListBox1.Items.Add(i)
            Next

            'now use setselected
            'assume only even are selected
            For i = 0 To 20
                If i Mod 2 = 0 Then
                    Me.ListBox1.SetSelected(i, True)
                End If
            Next

3rd edit

Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:

     Dim l As New List(Of Integer)

            l.Add(2)
            l.Add(6)
            l.Add(20)

You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:

 Dim i As Integer
        Dim l As New List(Of Integer)

        l.Add(2)
        l.Add(6)
        l.Add(20)

        'load the list with 20 numbers
        For i = 0 To 20
            Me.ListBox1.Items.Add(i)
        Next

        Dim lCount As Integer = 0

        For lCount = 0 To l.Count - 1
            For i = 0 To 20
                If i = l.Item(lCount) Then
                    Me.ListBox1.SetSelected(i, True)
                    Exit For
                End If
            Next
        Next

In the code my l is a list of just 3 items: 2, 6, and 20. I add these items to l which is just a list object. So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.

Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.

After running my code here is the result

enter image description here

Solution 2

You have to change the ListBox.SelectionMode property in order to enable multiple-selection.
The possible values are given by the SelectionMode enum, as follows:

None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections


So, you simply need to add the following line to the code you already have:

' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;

' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)

Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.

Share:
51,311
Richard.Gale
Author by

Richard.Gale

Updated on April 19, 2020

Comments

  • Richard.Gale
    Richard.Gale about 4 years

    I have a list box on a form and it works fine for what I want to do.

    I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.

    My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.

    PLease can someone give me some pointers.

    I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.

    Any help wil be much appreciated.

    My Code:

        Private Sub SelectItemSizes(ByVal itemID As Integer)
    
        Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
    
        Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
    
        If (itemSizes.Count > 0) Then
    
            For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
    
                For x As Integer = 0 To itemSizes.Count - 1
    
                    If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
                        Me.lstItemSizes.SetSelected(i, True)
                    Else
                        Me.lstItemSizes.SetSelected(i, False)
                    End If
    
                Next
    
            Next
    
        End If
    
    End Sub