Link to home
Start Free TrialLog in
Avatar of samsonite1023
samsonite1023

asked on

ListItem switching

What is the fastest way to swap a listview item (in report mode) with another (including all subitems, preferably properties)...

For now I have something like this:

private sub swapitems(x as single, y as single)

for p = 1 to listview1.listitems.listsubitems.count

  temp = listview1.listitems(x).listsubitems(p)
  listview1.listitems(x).listsubitems(p) = listview1.listitems(y).listsubitems(p)
  listview1.listitems(y).listsubitems(p) = temp

next p

'switch all properties...

end sub

This would swap each subitem of x and y, and then switch all their properties... but is there a faster way?

Thanks.

-sam
Avatar of tandrei
tandrei

try this

Private Sub SwapItems(x As Single, y As Single)
    Dim li As ListItem, aux As ListItem, aux1 As ListItem
   
    Set li = ListView1.ListItems.Item(x)
    Set aux = li
    Set aux1 = ListView1.ListItems.Item(y)
    Set li = ListView1.ListItems.Item(y)
    Set aux1 = li
End Sub



What about changing the INDEX property of the ListItem objects.  So that you swap the Indexes instead of the data....
Avatar of samsonite1023

ASKER

Tandrei, I couldn't get that to work.. have you tested it?  I switched some stuff around, and the best I could do is swap ListItem texts (just the first field, not the subitems)


Gmleeman, I believe the index property is read only.  Is there a way to change the index?

-Sam
I suspected it was read-only.  Sorry.  MSDN doc was not that specific...Was curious about Tandrei's approach also - although he could be on the right track...

Can you use two objects: ListItem  and SubItem, then swap the whole series of subitems by swapping just the SubItem objects? like Tandrei suggested except duplicated for the SubItem object.
It is a good idea, but even if it does work: how would I switch all the other attributes (i.e: bold, checked, ghosted, etc)

It's hard to believe you just can't set one listitem equal to another, and have them be identicle.

Please help!

-Sam
Try a variation of Tandreis' code where you use the NEW operator to make a NEW instance of a ListItem, perhaps that might work...
Would you be so kind as to show me example code?

Thanks

-Sam
This is Tandreis' example - so he gets the points if it works - haven't tried it. just an idea...adding "New" to the declarations may allow for dynamic allocation when you use the "=" equals sign...



Private Sub SwapItems(x As Single, y As Single)
   Dim li As New ListItem
   Dim aux As New ListItem
   Dim aux1 As New ListItem
   
   Set li = ListView1.ListItems.Item(x)
   Set aux = li
   Set aux1 = ListView1.ListItems.Item(y)
   Set li = ListView1.ListItems.Item(y)
   Set aux1 = li
End Sub

OR

try changing the lines to:
   Set li = NEW ListView1.ListItems.Item(x)




ASKER CERTIFIED SOLUTION
Avatar of gmleeman
gmleeman
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I haven't tried the code you have used (it is very similar to what I've tried before).

When you set ListItems equal to each other, it merely sets the .text properties of each of them equal, nothing else.


ListView1.ListItems.Item(x)= ListView1.ListItems.Item(y)

is identicle to:

ListView1.ListItems.Item(x).Text = ListView1.ListItems.Item(y).Text


I guess there is no way around this.. seems like MS should include a way to swap ListItems, other than a large chunk of code to copy each property.

Thanks

-Sam
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20260098.html
https://www.experts-exchange.com/questions/Q.20260039.html
https://www.experts-exchange.com/questions/Q.20260075.html
https://www.experts-exchange.com/questions/Q.20265244.html
https://www.experts-exchange.com/questions/Q.20265805.html
https://www.experts-exchange.com/questions/Q.20267117.html
https://www.experts-exchange.com/questions/Q.20272828.html
https://www.experts-exchange.com/questions/Q.20280182.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange