ListBox Multi-Select Issue

lfields20

Member
Joined
Sep 12, 2009
Messages
13
Programming Experience
1-3
When I use Shift+Click to select multiple items in a listbox, the following code is not recognizing the selected items between the first selected index and the last selected index as being selected:

VB.NET:
For i As Integer = 0 To listSongs.Items.Count - 1 Step 1
     MessageBox.Show("Index of " & i & " is selected: " & listSongs.GetSelected(i))
                
     If listSongs.GetSelected(i) Then
          Dim item As New listbox_items
          item.index = i
          selected_arr.Add(item)
     End If
Next

Here is the output of the message box when it gets to index 4 in the listbox.
selection_bug.jpg

The .GetSelected(index) As Integer returns true if the specified item is selected, so I believe I'm using the correct function.

Could someone tell me what I'm doing wrong or if there is a better way to determine if the specified index is selected or not?

Thanks
 
I haven't used the GetSelected function on the ListBox yet, but I have looped the SelectedIndex collection to get the selected items:
If ListBox1.SelectedIndices.Count > 0I Then
    For counter As Integer = 0I To ListBox1.SelectedIndices.Count - 1I
        MessageBox.Show(ListBox1.Items(ListBox1.SelectedIndices(counter)).ToString)
    Next counter
End If
 
Hi,

There is some weirdness going on in that picture. I can easily give you another option, shown below, to answer your question but I can not replicate or answer what you are displaying in that picture with the code that you have provided?? It should be working fine?

That said, here are two things for you:-

1) You only need to use the Step Keyword in a FOR loop when you need to change the DEFAULT stepping sequence from 1 to something different.

2) Another way to interact with the selected items in a ListBox is to use the SelectedItems property of the ListBox. here is an example:-

VB.NET:
Dim lstSelected As New List(Of String)
 
For Each strValue As String In listSongs.SelectedItems
  MsgBox(strValue & " Has Been Seleceted")
  lstSelected.Add(strValue)
Next

Hope that helps.

Cheers,

Ian
 
I answered this question on another forum is a similar manner to both JB and IR. One thing that you haven't mentioned though is where you put that code. Was it in the SelectedIndexChanged event handler? If not then that is probably the issue. If so then something is amiss and I'd suggest doing as I did and creating a test application to test just this one thing to see if it behaves the same way as your existing project.
 
I tried JuggaloBrotha's and Ian's code and it was giving the same results as the GetSelected function.

I was using it in the MouseUp event, which I believe that takes place after SelectedIndexChanged event.

I did try the code in the SelectedIndexChanged event and still giving the same results. I cannot see any reason for this not working?
 
Hi,

That is just weird. I have just tried this myself using the MouseUp event of the ListBox and all works fine for me?

The best I would suggest now is that, if this is not too big a project, then please upload your project as a zip file, ensuring that you get rid of any executable files in your Bin\Debug directories, and let us know where this is being used in the project so that we can investigate this further.

I am sure that everyone is as intrigued as I am to figure out what is going on here.

Cheers,

Ian
 
If you're interested in items selected then using the MouseUp event wouldn't make sense. The SelctedIndexChanged event is the obvious and correct choice. If that's not working for you either though, something is broken. Have you done as I believe I suggested elsewhere and created a test project to compare its behaviour with your existing project? It may be just that project that's broken or it may be your system.
 
One of the things I'm curious about is why is he getting the selected items and storing it in a list in the SelectedIndexChanged event. Normally you wouldn't gather the selected items as the user is selecting them, you do that when you actually need to know the items, like in a button's click event, or a toolstripmenuitem's click event. My code works perfectly in that scenario, I just can't think of a scenario where you wouldn't code it that way.
 
I'm capturing the list of selected items because when using drag and drop, all of the selected items are lost.

With my method, I can use the list to re-select the items before the drop occurs, which works just fine other than the problem with the selected items.

Maybe there is a better way to keep those items selected when using drag and drop?

I'm primarily a web developer with PHP so I'm not familiar with drag and drop events in VB.NET
 
John,

That did work, but the thing that worries me about that is most users are not going to know to do that.
Most users are going to release the shift key before dragging, as I always have so I don't think this is reliable.

jmcilhinney,
I created a test project and it was working as expected when selecting multiple items with the shift key.

I'm going to post all my listbox code to see if anybody can see what is wrong with it.

VB.NET:
Private Sub listSongs_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles listSongs.MouseDown
        'If there were any multiple selected items, then display them
        Dim sIndex As Integer = listSongs.IndexFromPoint(New Point(e.X, e.Y))
        If sIndex > -1 Then
            If ascan_listbox_iteam(sIndex) > -1 Then
                For i As Integer = 0 To selected_arr.Count - 1 Step 1
                    listSongs.SetSelected(selected_arr(i).index, True)
                Next
            Else
                listSongs.SetSelected(sIndex, True)
            End If
        End If

        Dim dragSize As Size = SystemInformation.DragSize
        dragSongRect = New Rectangle(New Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize)
    End Sub

    Private Sub listSongs_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles listSongs.MouseMove
        If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
            If (dragSongRect <> Rectangle.Empty AndAlso Not dragSongRect.Contains(e.X, e.Y)) Then
                source = listSongs.Name
                gridSchedule.DoDragDrop(gridSchedule, DragDropEffects.Copy)
            End If
        End If
    End Sub

    Private Sub listSongs_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles listSongs.MouseUp
        'Traps the selected indexes of the listSongs listbox for drag and drop
        If listSongs.SelectedItems.Count > 1 Then
            selected_arr.Clear()
            For i As Integer = 0 To listSongs.Items.Count - 1 Step 1
                'MessageBox.Show("Index of " & i & " is selected: " & listSongs.GetSelected(i))
                If listSongs.GetSelected(i) Then
                    Dim item As New listbox_items
                    item.index = i
                    selected_arr.Add(item)
                End If
            Next
        Else
            selected_arr.Clear()
        End If
    End Sub
 
I have figured out the solution:

In the following code: The bolded line is what was causing the problem. Thanks for the help.

If ascan_listbox_iteam(sIndex) > -1 Then
For i As Integer = 0 To selected_arr.Count - 1 Step 1
listSongs.SetSelected(selected_arr(i).index, True)
Next
Else
listSongs.SetSelected(sIndex, True)
End If
 
Back
Top