Moving tree nodes seems painfully slow

I’m working with a large TMSFNCTreeView; the sample data I’m working with has 70,000 nodes and I’ve run into speed issue when moving nodes (in order to sort them). That’s a lot of nodes, but I’m only sorting the sub-nodes that are immediately under the currently selected node. Level 1 of the treeview has just 150 nodes and it takes several minutes to sort it with a simple bubble sort. So I put together the following code which results in each node being moved just one time and yet it still takes about 15 seconds to sort those 150 nodes:

MyList:=tStringlist.create;
MyList.Sorted:=true;
TreeView1.BeginUpdate;
for y:=0 to (TreeView1.selectedNode.nodes.count-1) do
MyList.AddObject(TreeView1.selectedNode.nodes[y].text[0] TreeView1.SelectedNode.Nodes[y]);
for y:=0 to (MyList.count-1) do
TTMSFNCTreeViewNode(MyList.Objects[y]).MoveTo(TreeView1.SelectedNode);
TreeView1.EndUpdate;

(this is sorting alphabetically but I’ll need to sort on other criteria once this is working efficiently)
The delay is almost entirely in that second loop that’s just moving each of the 150 nodes to its correct index. It seems odd that the move process takes that long because it doesn’t take that long to create the nodes in the first place. Is there a more time-efficient way to change the order of these nodes?

Hi,

Initially when loading the node structure this is relatively fast, but then each subsequent interaction with nodes rewrites the node structure. To rebuild the node structure based on the collection, you can use

TreeView1.BeginUpdate;
TreeView1.ClearNodeList;
//Sort nodes
TreeView1.EndUpdate:

This should then rebuild the node structure once.

Thank you, that makes a huge difference.

ECFCC3589BD54BD4ACE61DB9A605B2A9.jpg

Gregg Van Oss

Wild Man Software

WildMan@ExactChange.info

(707)987-4499

1 Like