Solved

List boxs Item deletion

Posted on 2002-04-15
11
176 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Word macro - how to get characters after the searched for string 5 84
Hide vba in gp 7 94
How to measure sizes and angles in scanned images ? 3 63
TT Auto Dashboard 13 92
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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