My code is close but doesn't handle child nodes. It adds all nodes with the same indentation.
The selected nodes are saved when the user clicks the form's Copy button with:
NodeArray := VST.GetSortedSelection(False);
where NodeArray: TNodeArray; has been defined as a public variable for the form
In my example the VirtualNode only has one item in the record - "Caption" although this approach works fine with numerous fields in the record.
Those nodes are pasted with:
procedure TSystemForm.PasteBtnClick(Sender: TObject);
var
i : integer;
NewData, OldData: PTreeData;
Node: PVirtualNode;
begin
with VirtualStringTree do
begin
if Assigned(FocusedNode) then
Node := InsertNode(FocusedNode, amInsertAfter)
else
Node := AddChild(nil);
for i := Low(NodeArray) to High(NodeArray) do
begin
OldData := GetNodeData(NodeArray[i]);
NewData := GetNodeData(Node);
NewData^.Caption := OldData^.Caption;
if i <> High(NodeArray) then
Node := InsertNode(Node, amInsertAfter);
end; // for i
ValidateNode(FocusedNode, False);
end; // with
end;
The goal is to have the initial node added to the same level as the selected "FocusedNode" and then utilize the parent/child properties of the copied nodes for subsequent new nodes. It seems that the code on line 19 in this code might be changed to something like this pseudo code.
if add to same level then
Node := InsertNode(Node, amInsertAfter)
else // add indented
Node := InsertNode(Node, amAddChildLast);
This approach seems very difficult to handle with child nodes moving in and out. How can I retain the proper parent/child relationships in the added nodes?