• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Removing Multiple Selected Items from a ListView

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
Stephent
Asked:
Stephent
  • 2
1 Solution
 
magglass1Commented:
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
 
JoaTexCommented:
Hi
try to make in reverse Way.

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

Jo
0
 
magglass1Commented:
Forgot to put Step -1 in the For loop in my reply.  Sorry.
0
 
StephentAuthor Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now