Solved

List boxs Item deletion

Posted on 2002-04-15
11
173 Views
Last Modified: 2012-02-13
how can i delete multiple selected items in List Box?

Thanks,
shri
 
0
Comment
Question by:shrihalbe
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 20 total points
ID: 6942782
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
 

Author Comment

by:shrihalbe
ID: 6942795
Hello RItchie,
it doesn't work.
 if we delete any item then list index get changed.
Thanks,
shri
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 6942796
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6942806
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
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6944103
Joe Griffith is right
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:TechJD
ID: 6945164
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6945242
Hi shrihalbe, any progress/feedback?
0
 

Author Comment

by:shrihalbe
ID: 6945409
Hello
  the problem has been solved. i used Joe_Griffith Code.

thanks
shri
0
 
LVL 1

Expert Comment

by:TechJD
ID: 6945503
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6945735
then, accept his comment as answer and close this thread.
0
 

Author Comment

by:shrihalbe
ID: 7579539
THANKS,

SHRI
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now