The easiest way to do this is recursively, something like this:
procedure WalkChildren(Node: TTreeNode; List: TList<TTreeNode>); var i: Integer; begin List.Add(Node); for i := 0 to Node.Count-1 do begin WalkChildren(Node[i], List); end; end;
Name it as follows:
List := TList<TTreeNode>.Create; WalkChildren(Root, List);
This example adds to the list, but you can replace this action with one of your options.
Now that we can walk in one tree, it is time to walk in two trees in tandem.
procedure CopySubTree(Src, DestParent: TTreeNode; DestNodes: TTreeNodes); var i: Integer; Dest: TTreeNode; begin Dest := DestNodes.AddChild(DestParent, Src.Text); //copy other parameters from Src to Dest as you please for i := 0 to Src.Count-1 do begin CopySubTree(Src[i], Dest, DestNodes); end; end;
You can call it like this:
CopySubTree(SrcTree.Selected, nil, DestTree.Items);
source share