I’m working with the TMSFNCTreeView and I need to sort all the subnodes under the currently selected node according to a custom sorting algorithm. I’d been using TTMSFNCTreeViewNode.MoveTo and that seemed to be working well, but something has changed and now I’m getting an access violation when I try that.
Below is the minimum code example for what I’m trying to do. It adds the text of the nodes to a sorted stringlist and then moves the nodes according to their order in the stringlist (sorting alphabetically isn’t my goal, but I can replace that simple logic with what I need once I can get the nodes moving without getting an access violation).
Is the MoveTo method the correct way to rearrange nodes? I also tried setting TreeView1.selectedNode.Collection.Items[x].Index to move the nodes; that didn’t give an access violation but it does not seem to effect the order of the subnodes.
procedure TMainForm.Sortsubnodesalphabetically1Click(Sender: TObject);
var y,x:integer;
SortedList: tStringlist;
begin
SortedList:=tStringlist.create;
SortedList.Sorted:=true;
TreeView1.BeginUpdate;
TreeView1.ClearNodeList;
for y:=0 to (TreeView1.selectedNode.Collection.Count-1) do
SortedList.AddObject(TTMSFNCTreeViewNode(TreeView1.selectedNode.Collection.Items[y]).text[0], Pointer(y));
for y:=0 to(SortedList.count-1) do
begin
x:=Longint(SortedList.Objects[y]);
TreeView1.selectedNode.nodes[x].MoveTo(TreeView1.selectedNode,y);
end;
TreeView1.EndUpdate;
SortedList.Free;
end;