dudup
asked on
TVirtualStringTree copy between two trees
Hi,
I need an example code to copy a node and all it's childs from one TVirtualStringTree to another TVirtualStringTree. I am thinking of using a dummy TVirtualStringTree to temporarily store the copied nodes and later copy it to another TVirtualStringTree.
So, here is the task:
1. Select a source node in TREE_A
2. Delete all nodes in the DUMMY_TREE
3. Copy the source node and it's childs from TREE_A to the DUMMY_TREE
4. Select a target node in TREE_B
5. Copy the copied node and it's childs from DUMMY_TREE to the TREE_B target node (as it's child of course).
I think the task is quite clear. Anyone can help with the code? The use of DUMMY_TREE is a must because TREE_A and TREE_B could only be available one at a time.
I dont need a complete working code, but to show how delete all nodes in dummy tree, to copy node and it's childs to dummy tree, and later copy it to TREE_B.
Thanks.
I need an example code to copy a node and all it's childs from one TVirtualStringTree to another TVirtualStringTree. I am thinking of using a dummy TVirtualStringTree to temporarily store the copied nodes and later copy it to another TVirtualStringTree.
So, here is the task:
1. Select a source node in TREE_A
2. Delete all nodes in the DUMMY_TREE
3. Copy the source node and it's childs from TREE_A to the DUMMY_TREE
4. Select a target node in TREE_B
5. Copy the copied node and it's childs from DUMMY_TREE to the TREE_B target node (as it's child of course).
I think the task is quite clear. Anyone can help with the code? The use of DUMMY_TREE is a must because TREE_A and TREE_B could only be available one at a time.
I dont need a complete working code, but to show how delete all nodes in dummy tree, to copy node and it's childs to dummy tree, and later copy it to TREE_B.
Thanks.
ASKER
No, I dont need to display the dummy tree.
I dont know if TList would work, how about all the child nodes?
Because I dont think you can retain the "tree" structure in the TList.
I dont know if TList would work, how about all the child nodes?
Because I dont think you can retain the "tree" structure in the TList.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BtoDummy has to DummytoB
but I think you got that one ?
but I think you got that one ?
ASKER
Hmm, I just read somewhere. I found there are 2 interesting properties:
ClipboardFormats and DefaultPasteMode ...
Can we use that for this task? That means the DUMMY_TREE = Clipboard
ClipboardFormats and DefaultPasteMode ...
Can we use that for this task? That means the DUMMY_TREE = Clipboard
ASKER
Hi Geert,
Your code is interesting, I will try that.
Your code is interesting, I will try that.
ASKER
If you define:
VirtualStringTreeA: TVirtualStringTree;
Dont you need to CREATE it in the FormCreate?
VirtualStringTreeA: TVirtualStringTree;
Dont you need to CREATE it in the FormCreate?
you may take a look here
https://www.experts-exchange.com/questions/20407731/load-save-ado-into-from-treeview.html#7873367
meikl ;-)
https://www.experts-exchange.com/questions/20407731/load-save-ado-into-from-treeview.html#7873367
meikl ;-)
it was created with the Form Designer
drop a VirtualStringTree on the form 3x times
and rename them to A, B and Dummy
drop a VirtualStringTree on the form 3x times
and rename them to A, B and Dummy
ASKER
when I do copy TO the dummy_tree, I got error: Control " has no parent window
procedure CopyNode(aSourceNode, aTargetParentNode: PVirtualNode; WithSiblings, WithChildren: boolean);
var
nNode, mNode: PVirtualNode;
aRec, bRec: PTreeData;
begin
nNode := aSourceNode;
while Assigned(nNode) and ValidNode(nNode) do
begin
mNode := TargetTree.AddChild(aTarge tParentNod e); // <----- error
procedure CopyNode(aSourceNode, aTargetParentNode: PVirtualNode; WithSiblings, WithChildren: boolean);
var
nNode, mNode: PVirtualNode;
aRec, bRec: PTreeData;
begin
nNode := aSourceNode;
while Assigned(nNode) and ValidNode(nNode) do
begin
mNode := TargetTree.AddChild(aTarge
ASKER
In my case, I define the dummy_tree manually as public variable of TForm1:
dummy_tree: TVirtualStringTree;
..... in the FormCreate:
dummy_tree := TVirtualStringTree.nil;
dummy_tree: TVirtualStringTree;
..... in the FormCreate:
dummy_tree := TVirtualStringTree.nil;
ASKER
Fixed the error:
dummy_tree: TVirtualStringTree(Self);
dummy_tree.Parent := Self;
dummy_tree.visible := FALSE;
dummy_tree: TVirtualStringTree(Self);
dummy_tree.Parent := Self;
dummy_tree.visible := FALSE;
ASKER
Hmm, the copy from dummy_tree to tree_b does not work well.
It does not copy to the tree_b.focusednode, always in the "root".
Something to do with the "nil" --> CopyNode(SourceNode, nil, False, True);
It does not copy to the tree_b.focusednode, always in the "root".
Something to do with the "nil" --> CopyNode(SourceNode, nil, False, True);
ASKER
Geert, I think thats because the "TargetNode" in the CopyNodesToTree is not used at all....
And I cannot figure out where to put it.
And I cannot figure out where to put it.
.nil ?
dummy_tree := TVirtualStringTree.Create( Self);
dummy_tree.NodeDataSize := SizeOf(RRec);
dummy_tree.Parent := Self;dummy_tree.
dummy_tree.OnGetText :=VirtualStringTreeAGetTex t;
dummy_tree.Visible := False;
this will probably work a little better
dummy_tree := TVirtualStringTree.Create(
dummy_tree.NodeDataSize := SizeOf(RRec);
dummy_tree.Parent := Self;dummy_tree.
dummy_tree.OnGetText :=VirtualStringTreeAGetTex
dummy_tree.Visible := False;
this will probably work a little better
especially don't forget the NodeDataSize ...
ASKER
Found it !
---> CopyNode(SourceNode, TargetNode, False, True);
---> CopyNode(SourceNode, TargetNode, False, True);
it did need a little tweaking :)
ASKER
Thanks Geert :) It works ... You are the best, especially for TVirtualStringTree !
ASKER
There was a bug in the code, but it is easy to fix :)
uhm, i don't think so, but i'm not argueing :)
if not you could probalby use a TCollection or TList to store the data