Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Did you set the selectionmode to multi?</p> <p>You need to specify that in order to allow multiple selections.</p> <p>Then you can do:</p> <pre><code>Dim i as Integer=0 For i=0 To Me.listBox.SelectedItems.Count -1 'display the listbox value next i </code></pre> <p>Here is a screen shot:</p> <p><img src="https://i.stack.imgur.com/OFM7c.png" alt="enter image description here"></p> <p>After you set the property on the listbox then call setselected based on the values you want selected.</p> <pre><code>me.lstItemSizes.SetSelected(3,true) me.lstItemSizes.SetSelected(4,true) me.lstItemSizes.SetSelected(9,true) </code></pre> <p>Here you can add 20 numbers and only select the even.</p> <pre><code> 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 </code></pre> <h1>3rd edit</h1> <p>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:</p> <pre><code> Dim l As New List(Of Integer) l.Add(2) l.Add(6) l.Add(20) </code></pre> <p>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:</p> <pre><code> 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 </code></pre> <p>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.</p> <p>Notice in my for loop that once the item in my list is found I no longer need to loop so I <code>exit for</code>. 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.</p> <p>After running my code here is the result</p> <p><img src="https://i.stack.imgur.com/sJsNG.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload