-
I can't figure it out anymore :p
--
My prob:
The user can add files to a listbox to let 'm run in the program (in a batch-sequence) but ofcourse I have to let the user delete files he doesn't need to be parsed.
but....
when I try to delete a item in a selected() way it gives me an error..
--
If lList.ListCount = 1 Then
lList.RemoveItem (lList.ListIndex)
Else
For I = 0 To (lList.ListCount)
If lList.Selected(I) = True Then
lList.RemoveItem (I)
End If
Next I
End If
Most of the time if I = 2 then it crashes...
What am I doin' wrong here?
Thnx in advance;
-
the items are indexed so that the 0 counts and the last one is lListcount -1
Code:
For I = 0 To (lList.ListCount) -1
If lList.Selected(I) = True Then
lList.RemoveItem (I)
End If
Next I
-
Tried... Crashed.
I solved it in a very cruel way though :)
--
On Error Resume Next
lList.ListIndex = -1
For I = -1 To (lList.ListCount - 1)
If lList.Selected(I) = True Then lList.RemoveItem I
Next I
I = 0
If lList.Selected(I) = True Then lList.RemoveItem I
On Error GoTo 0
--
It crashes on the Selected(I) (I is that at that point 0).. so I presume it crashes because the list index is updated at that point and that the Index says there is a 2 but it's now 1 or 0
(removing more then one option)
Did that make sense? :)
-
Other code?
What other Subs you have that might affect when item is selected or something?
If there's any, you should do somehow so that nothing else wouldn't be checked when deleting items.
Hope this helps,
-
Nothing interferes with the list box except the sub mentioned above.
-
The way to remove multiple items from a listbox is to loop backwards thru the listbox - otherwise, your loop counter will get out of sync with the listindexes and cause you the problems that you describe.
Try this:
Code:
For I = (lList.ListCount -1) To 0 Step -1
If lList.Selected(I) = True Then
lList.RemoveItem (I)
End If
Next I
-
That should do the trick; Thank you.