Problem with Multis Selct ListBox on Remove...

Me below code runs on lstListBox1_DoubleClick1 it's job is to move items from List one over to list 2
and then remove them from List1 so the user can not double select them. On a single select this code
works fine but if i do this with a multi select it trows an error saying "index is outside of the Bounds"
If i disable the remove command the code works fine


Dim i As Int32
        For i = 0 To lstListBox1.SelectedItems.Count - 1

            lstListBox2.Items.Add(lstListBox1.SelectedItems(i).ToString())
            lstListBox1.Items.Remove(lstListBox1.SelectedItems(i).ToString())
        Next
AlexPonnathAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        While lstListBox1.SelectedItems.Count > 0
            lstListBox2.Items.Add(lstListBox1.SelectedItems(0).ToString())
            lstListBox1.Items.Remove(lstListBox1.SelectedItems(0).ToString())
        End While
    End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.