Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

List boxs Item deletion

how can i delete multiple selected items in List Box?

Thanks,
shri
 
0
shrihalbe
Asked:
shrihalbe
  • 4
  • 3
  • 2
  • +2
1 Solution
 
Richie_SimonettiIT OperationsCommented:
Private Sub Form_Click()
On Error Resume Next
Dim i As Long
For i = 1 To List1.ListCount
    If List1.Selected(i) Then
       List1.RemoveItem i
    End If
Next i

End Sub
0
 
shrihalbeAuthor Commented:
Hello RItchie,
it doesn't work.
 if we delete any item then list index get changed.
Thanks,
shri
0
 
Joe_GriffithCommented:
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

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Richie_SimonettiIT OperationsCommented:
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
0
 
corvanderlindenCommented:
Joe Griffith is right
0
 
TechJDCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
Hi shrihalbe, any progress/feedback?
0
 
shrihalbeAuthor Commented:
Hello
  the problem has been solved. i used Joe_Griffith Code.

thanks
shri
0
 
TechJDCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
then, accept his comment as answer and close this thread.
0
 
shrihalbeAuthor Commented:
THANKS,

SHRI
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now