How to remove multiple selected items in ListBox - Visual Basic 2017

Hot

Thursday, October 26, 2017

How to remove multiple selected items in ListBox

To explain better how to remove multiple selected items in ListBox I will use the form structure from the previous post "How to remove selected item in Listbox" with little changes. Plus I will add an option for multi selection in Listbox and additional button for multi deletion.

Add items in Listbox
        ListBox1.Items.Add("some text")
        ListBox1.Items.Add("some text 1")
        ListBox1.Items.Add("some text 2")
        ListBox1.Items.Add("some text 3")
        ListBox1.Items.Add("some text 4")

To add multi selection in Listbox I added a code in Form1_Load event

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.SelectionMode = SelectionMode.MultiSimple
End Sub

Remove multiple selected items in Listbox code

       For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndices.Item(i))
        Next

How to remove multiple selected items in ListBox

No comments:

Post a Comment