3

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
Richard Gale
  • 1,816
  • 5
  • 28
  • 45
  • Its this piece of code thats causing the problem: 'Else Me.lstItemSizes.SetSelected(i, False)' – Richard Gale Mar 30 '11 at 15:33
  • @Richard- no its the way you are looping, you should loop based on the count of your item size first, then the count of your items in your list box. See my 3rd edit in my example – JonH Mar 30 '11 at 15:37

4 Answers4

9

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

JonH
  • 32,732
  • 12
  • 87
  • 145
  • I want to display all of the possible sizes but only select the sizes which relate to the item. eg. the list may contain 5 sizes but only 3 of them relate to the item I am loading. – Richard Gale Mar 30 '11 at 15:19
  • @Richard, right you can do that but first in the properties of the list box you must ensure that `SelectionMode` is set to multisimple for instance. – JonH Mar 30 '11 at 15:20
  • I have pasted my code with the question. SelectionMode is set to MultiExtended – Richard Gale Mar 30 '11 at 15:23
  • @Richard - Did you debug and find out why this is happening, your answer is right there, if you try to debug it. – JonH Mar 30 '11 at 15:27
3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

According to MSDN, SetSelected() can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:

' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)

For reference, this is the MSDN article.

Ender
  • 14,995
  • 8
  • 36
  • 51
0

Because my code had the following loops:

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

The first loop loops through the available sizes and the second loop is used to compare the item sizes.

Having the following code:

Else
 Me.lstItemSizes.SetSelected(i, False)
End If

Meant that even if item i became selected, it could also be deselected.

SOLUTION: Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For

Richard Gale
  • 1,816
  • 5
  • 28
  • 45
  • @Richard- Just so you know you probably don't need to create an answer to this, just state in the comments that you just needed the exit for. I am glad you got your issue resolved though! – JonH Mar 30 '11 at 15:50