
Removing items from ListView en masse
hi robert,
Quote:
> On the For Each Item In ListView1, I receive:
the syntax is for each OBJECT in COLLECTION and the collection is
ListView1.ListItems (ListView1 isn't a collection)
if you loop through the collection, you can't modify it (remove items).
runtime error (35606 Control's collection has been modified)
look the code below, this is a possible solution for your problem.
the code stores all selected items in a listitem-array.
loop through the listitem-array, add the items to listview2 and remove them
from listview1.
Dim i As Integer
Dim itmX As ListItem
Dim arrX() As ListItem
i = 0
ReDim arrX(0)
For Each itmX In ListView1.ListItems
If itmX.Selected Then
i = i + 1
ReDim Preserve arrX(UBound(arrX) + 1)
Set arrX(i) = itmX
End If
Next
With ListView2.ListItems
For i = 1 To UBound(arrX)
Set itmX = .Add(, , arrX(i).Text)
itmX.Key = arrX(i).Key
ListView1.ListItems.Remove (arrX(i).Index)
Next i
End With
hope this helps.
regards, pietro
Quote:
> On the For Each Item In ListView1, I receive:
> "Object Doesn't Support This Property or Method"
> If I remove the Dim Item statement, I receive:
> "Variable Not Defined"
> Also, ListView1.MultiSelect = True, so not all of the items will be
removed
> when the user adds them en masse to ListView2.
> So it's like:
> For Each *Selected* Item in ListView1
> Add item to ListView2
> Remove Item from ListView1
> Any ideas?
> Robert
> : hi robert,
> :
> : don't remove each item of ListView1. use the clear-method.
> : and use the with-construct to add the items
> :
> : With ListView2.ListItems
> : For Each Item in ListView1
> : Set itmX = .Add()
> : Next
> : End With
> :
> : ListView1.ListItems.Clear
> :
> : hope this helps
> : regards, pietro
> :