Link to home
Start Free TrialLog in
Avatar of shrihalbe
shrihalbe

asked on

List boxs Item deletion

how can i delete multiple selected items in List Box?

Thanks,
shri
 
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shrihalbe
shrihalbe

ASKER

Hello RItchie,
it doesn't work.
 if we delete any item then list index get changed.
Thanks,
shri
You will avoid a lot of problems if you remove items from the bottom up and also use a zero-based index:

For i = List1.ListCount - 1 to 0
  If List1.Selected(i) Then
    List1.RemoveItem i
  End If
Next

NO. For that reason i used On error resume next
Try this:

Private Sub Form_Click()
Dim i As Integer
With List1
    Do
        If .Selected(i) Then
           .RemoveItem i
        End If
        i = i + 1
    Loop Until i = .ListCount
End With
   
End Sub
Joe Griffith is right
try this to remove with no errors

Private Sub Command1_Click()
Dim x As Integer
Dim i As Integer
i = List1.ListCount

For x = 0 To i - 1 ' list count always starts with 0 and i will be greater then you last item

If x >= i Then Exit For ' when you remove last item if it an odd number x
                         'is greater then i and if it's even it's = to i
 If List1.Selected(x) = True Then
      List1.RemoveItem x
  End If
 i = List1.ListCount ' you removed one i is smaller
 Next x

End Sub
Hi shrihalbe, any progress/feedback?
Hello
  the problem has been solved. i used Joe_Griffith Code.

thanks
shri
Can someone explane why Joe_Griffith's code dont do anything
in my VB6
I tried it and even add a break point and ran through it
with F8 step
and it just stops on For i = List1.ListCount - 1 To 0
dont do anything else
then, accept his comment as answer and close this thread.
THANKS,

SHRI