0

I'm having a little trouble deleting multiple lines for a listbox. I know how to remove a single line, which is this:

Try
            If listbox1.Items.Count <> 0 Then
                Dim linedel As Integer
                linedel = listbox1.Items.IndexOf(listbox1.SelectedItem)
                listbox1.Items.RemoveAt(linedel)
            Else
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

If I change the value for IndexOf to SelectedItems, I get an error stating that -1 is not valid for index.

I am guessing this needs to be done using an integer array and a for each but I don't know how to do that. I really never bothered to look much into arrays.

Thanks.

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Oxiegen
0

Try this:

Try
            If listbox1.Items.Count > 0 Then
                If listbox1.SelectedIndices.Count > 0 Then
                    For Each index As Integer In listbox1.SelectedIndices
                        listbox1.Items.RemoveAt(index)
                    Next
                End If
            Else
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.