Solved

List boxs Item deletion

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

Thanks,
shri
 
0
Comment
Question by:shrihalbe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

690 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