?
Solved

Removing Multiple Selected Items from a ListView

Posted on 2003-02-27
4
Medium Priority
?
150 Views
Last Modified: 2012-05-04
Hi

How do I remove multiple selected items from a ListView.

I have two listviews. One procedure copies mutliple selections from one listview to the second listview. If I then want to removed multiple selections from the second list view I have the following code

Private Sub cmdRemove_Click()
Dim i As Integer
Dim a As Integer

For i = 1 To lvplan.ListItems.Count
    If lvplan.ListItems(i).Selected = True Then
        a = lvplan.ListItems(i).SubItems(2)
        lvActivityLog.ListItems(a).SmallIcon = 2
        lvplan.ListItems.Remove (i)
    End If
Next

End Sub

The problem occures because when one of the items is removed. For example if I have three items and I am loop through them and remove one then when I come to the third the index no longer exists and I get
Index Out of Bounds
Error message.

I'm I going about it the wrong way? High points because I need a quick answer.

Cheers,
Stephen
0
Comment
Question by:Stephent
[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
  • 2
4 Comments
 
LVL 1

Expert Comment

by:magglass1
ID: 8034781
Try reversing the for loop so that it removes items at the end first.  Something like...

Private Sub cmdRemove_Click()
Dim i As Integer
Dim a As Integer

For i = lvplan.ListItems.Count To 1
   If lvplan.ListItems(i).Selected = True Then
       a = lvplan.ListItems(i).SubItems(2)
       lvActivityLog.ListItems(a).SmallIcon = 2
       lvplan.ListItems.Remove (i)
   End If
Next

End Sub
0
 
LVL 2

Accepted Solution

by:
JoaTex earned 300 total points
ID: 8034824
Hi
try to make in reverse Way.

for i = lvplan.listitems.count to 1 step -1
...
Next

Jo
0
 
LVL 1

Expert Comment

by:magglass1
ID: 8034867
Forgot to put Step -1 in the For loop in my reply.  Sorry.
0
 
LVL 1

Author Comment

by:Stephent
ID: 8040472
Cheers Jo it worked excellently.

Sorry magglass few minutes earlier and the points were yours. Shame I can't split the points.

Stephen
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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 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