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.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.