Solved

List boxs Item deletion

Posted on 2002-04-15
11
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Joe Griffith is right
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:TechJD
Comment Utility
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
Comment Utility
Hi shrihalbe, any progress/feedback?
0
 

Author Comment

by:shrihalbe
Comment Utility
Hello
  the problem has been solved. i used Joe_Griffith Code.

thanks
shri
0
 
LVL 1

Expert Comment

by:TechJD
Comment Utility
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
Comment Utility
then, accept his comment as answer and close this thread.
0
 

Author Comment

by:shrihalbe
Comment Utility
THANKS,

SHRI
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA: Add rows to listbox based on criteria 7 62
Access 2016 VB code 9 85
VBA error replacing data 6 34
How to debug this code 7 49
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now