Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Select List Box

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
like image 402
Richard.Gale Avatar asked Apr 17 '26 23:04

Richard.Gale


1 Answers

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

like image 128
JonH Avatar answered Apr 19 '26 22:04

JonH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!