Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transferring records from one listview to another 1

Status
Not open for further replies.

tcode

Programmer
Jan 8, 2004
15
0
0
US
I'm trying to transfer records from one listview control to another. Here's the logic:

Dim itmX as listItem
For Each itmX In lvwGrps.ListItems
If itmX.Selected = True Then
PopulateGroupAdd itmX.Text, itmX.SubItems(1)
lvwGrps.ListItems.Remove itmX.Index
End If
Next

Problem:
It will populate the 2nd listview and delete the record from the 1st listview, but as it goes to the next record, I'm receiving an error that states: "Controls collection has been modified"
This would be the lvwGrps.listitems collection and then I receive "For loop not initialized"

I've tried various ways and can't seem to get all records to work properly. The user must be able to muli-select records for this transfer.

Any assistance is greatly appreciated!
 
i would do it a little different:

Code:
Dim i as integer 'used for the counter
Dim itmx as ListItem

For i = 1 To lvwGrps.ListItems.Count
  if lvwGrps.ListItems(i).Selected = True Then
    Set itmx = lvwDest.ListItems.Add(Key:=somekey, Text:=lvwGrps.ListItems(i).Text)
    lvwGrps.ListItems.Remove i
  end if
Next
adapt the assigning of the new listitem to fit your PopulateGroupAdd function...
 
Thanks for the response. It is much appreciated...
The problem that I'm having is in the "for loop", however. Your solution creates an out of bounds error.

If there are 26 original records and I transfer 3 of them out to another listbox, the "for loop" is still going to iterate through the list of records 26 times and at the 24th iteration, an index out of bounds error will occur because there are no records with an index past 23.

Any ideas?

Thanks again
 
reverse the loop...

Code:
For i =  lvwGrps.ListItems.Count to 1 step -1

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top