MAX.RAY MAX.RAY - 3 months ago 16
Vb.net Question

How to remove a random item from a ListBox

I can't remove a random item from

ListBox


I tried this but it doesn't work

Dim rnd As New Random
Dim randomIndex As Integer = rnd.Next(0, ListBox1.Items.Count)
ListBox1.Items.Remove(randomIndex)

Answer

The Remove method takes the actual item to remove, not the index. To remove by index, use RemoveAt:

Dim rnd As New Random
Dim randomIndex As Integer = rnd.Next(0, ListBox1.Items.Count)
ListBox1.Items.RemoveAt(randomIndex)
Comments