Reputation: 21
I want to delete Multiple rows of listbox, I tried this
For teller = 0 To 170 Step 1
ListBox1.Items.Remove(teller)
Next
But that doesn't work. How do I delete the first 170 rows in my listbox? :)
Upvotes: 2
Views: 4559
Reputation: 331
Try this
Dim count as Integer
count = ListBox1.Items.Count ''Or 170
For teller = 1 To count Step 1
ListBox1.Items.RemoveAt(0)
Next
EDIT As minitech stated it is always a good idea to remove the first item if you want to clear out a list or grid or any other collection of the first X items.
Upvotes: 1
Reputation: 44931
The problem is that as you remove items, your list becomes shorter, so you will run out of items. So, just remove the first record on each pass:
Updated to reflect minitech's comments
For teller = 0 To 169
ListBox1.Items.RemoveAt(0)
Next
Upvotes: 2
Reputation: 224867
Since removing an item from a list shifts the rest of them up, simply remove the first item multiple times:
For i As Integer = 1 To 170
Me.ListBox1.Items.RemoveAt(0)
Next
You were also using Remove
instead of RemoveAt
, which removes an item by reference, not by index.
Upvotes: 2