Reputation: 12183
I am trying to change the index of a node, because there are some specific nodes that at all times needs to be at the bottom of my tree. I tried to change the Node.Index, but that did not change anything. So my question is: How do I change the Index of a PVirtualNode?
Thanks! - Jeff
Upvotes: 1
Views: 2986
Reputation: 613612
Given that you are still using the tree view control as a container, the ideal solution offered by Smasher is not available to you.
One rather obvious solution, given that your tree view has no hierarchy (i.e. it's a list) would be to use the Sort
method with your own compare function (OnCompareNodes
).
The other blindingly obvious strategy would be to add the node that you want at the bottom last. If you need to add other nodes later, then insert them above the special last node with InsertNode
. This simple approach will probably suffice for the problem as you have described it.
Upvotes: 2
Reputation: 163357
To change the index of node A, find the node B that has the index you want A to have, and then call Tree.MoveTo(A, B, amInsertBefore, False)
. B and everything after it will shift down by one to make room for A, and their Index
fields will be recalculated. This works even if A doesn't yet exist in the tree (such as just after calling MakeNewNode
).
If you're using Index
to associate each node with its corresponding data value in a list or array, then you'll find this largely ineffective for re-ordering the displayed values.
Upvotes: 4
Reputation: 25678
TVirtualNode is a doubly-linked list, it's not a index-based structure: you change the index of a node by removing it and adding it where you want it.
Look into DeleteNode
and AddChild
.
Upvotes: 2
Reputation: 16620
You canot change the index of a node. Normally when using VirtualStringTree
you hold your data in your own data structure separate from the tree and access the data from the events.
You can also store data directly in the nodes (using a record), but I prefer the other approach because it keeps the logic out of the tree view.
For example, you could store the data in a list and access this list in the GetText
handler (you can use Node.Index
). Then, if you want to reorder the items, just reorder your list and everything else will happen automatically (you might have to call Invalidate
on the tree).
Pseudocode:
Initializing:
Tree.RootNodeCount := MyList.Count;
In the GetText
event:
NodeText := MyList [Node.Index];
Reordering:
Reorder (MyList);
Tree.Invalidate;
Upvotes: 3