Answered by:
How to delete list items?

Question
-
How can I delete an item of a list?Saturday, November 16, 2013 6:38 PM
Answers
-
Do you mean to delete the selected item?
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
Or to clear the whole listbox
ListBox1.Items.Clear()
- Proposed as answer by lifewcody Saturday, November 16, 2013 7:01 PM
- Marked as answer by Leo (Apple) Yang Friday, November 22, 2013 3:17 AM
Saturday, November 16, 2013 6:47 PM -
Hi,
I will guess that you may be using a List(Of T). If so you can use the Remove, RemoveAt, or RemoveRange to remove items from the list. Here is an example of them.
Dim MyList As New List(Of String) MyList.Add("Item-1") MyList.Add("Item-2") MyList.Add("Item-3") 'You can choose which way you want to remove the item(s) MyList.Remove("Item-2") 'Removes first occurrence of "Item 2" in the list MyList.RemoveAt(1) 'Removes the item at the index specified. Removes "Item-2" MyList.RemoveRange(1, 2) 'Removes a range of indexes. Removes "Item-2 and Item-3"
Here is the msdn link to the List(Of T) class methods. Click on the the Remove, RemoveAt, and RemoveRange to read more info about using them.
- Edited by IronRazerz Saturday, November 16, 2013 7:16 PM
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:32 AM
- Marked as answer by Leo (Apple) Yang Friday, November 22, 2013 3:17 AM
Saturday, November 16, 2013 7:06 PM
All replies
-
Do you mean to delete the selected item?
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
Or to clear the whole listbox
ListBox1.Items.Clear()
- Proposed as answer by lifewcody Saturday, November 16, 2013 7:01 PM
- Marked as answer by Leo (Apple) Yang Friday, November 22, 2013 3:17 AM
Saturday, November 16, 2013 6:47 PM -
Hi,
I will guess that you may be using a List(Of T). If so you can use the Remove, RemoveAt, or RemoveRange to remove items from the list. Here is an example of them.
Dim MyList As New List(Of String) MyList.Add("Item-1") MyList.Add("Item-2") MyList.Add("Item-3") 'You can choose which way you want to remove the item(s) MyList.Remove("Item-2") 'Removes first occurrence of "Item 2" in the list MyList.RemoveAt(1) 'Removes the item at the index specified. Removes "Item-2" MyList.RemoveRange(1, 2) 'Removes a range of indexes. Removes "Item-2 and Item-3"
Here is the msdn link to the List(Of T) class methods. Click on the the Remove, RemoveAt, and RemoveRange to read more info about using them.
- Edited by IronRazerz Saturday, November 16, 2013 7:16 PM
- Proposed as answer by Frank L. Smith Sunday, November 17, 2013 12:32 AM
- Marked as answer by Leo (Apple) Yang Friday, November 22, 2013 3:17 AM
Saturday, November 16, 2013 7:06 PM -
I tried the
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
but it didn't work.Any other ideas of how to delete the selected item of a list?
Friday, November 22, 2013 5:45 PM -
I tried the
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
but it didn't work.Any other ideas of how to delete the selected item of a list?
The above code works but you need put it in some action so for example you can do it like that:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged If (ListBox1.SelectedIndex <> -1) Then ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) End If End Sub
Or like that:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If (ListBox1.SelectedIndex <> -1) Then ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) End If End Sub
Friday, November 22, 2013 6:03 PM