Link to home
Start Free TrialLog in
Avatar of dudup
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.
Avatar of Geert G
Geert G
Flag of Belgium image

do you have to display the dummy tree ?
if not you could probalby use a TCollection or TList to store the data
Avatar of dudup
dudup

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.
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BtoDummy has to DummytoB
but I think you got that one ?
Avatar of dudup

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

Avatar of dudup

ASKER

Hi Geert,

Your code is interesting, I will try that.
Avatar of dudup

ASKER

If you define:

VirtualStringTreeA: TVirtualStringTree;

Dont you need to CREATE it in the FormCreate?
it was created with the Form Designer
drop a VirtualStringTree on the form 3x times
and rename them to A, B and Dummy

Avatar of dudup

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(aTargetParentNode);     // <----- error
Avatar of dudup

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;
Avatar of dudup

ASKER

Fixed the error:

dummy_tree: TVirtualStringTree(Self);
dummy_tree.Parent := Self;
dummy_tree.visible := FALSE;
Avatar of dudup

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);
Avatar of dudup

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.
.nil ?
dummy_tree := TVirtualStringTree.Create(Self);
dummy_tree.NodeDataSize := SizeOf(RRec);
dummy_tree.Parent := Self;dummy_tree.
dummy_tree.OnGetText :=VirtualStringTreeAGetText;
dummy_tree.Visible := False;
this will probably work a little better



especially don't forget the NodeDataSize ...
Avatar of dudup

ASKER

Found it !

---> CopyNode(SourceNode, TargetNode, False, True);
it did need a little tweaking :)
Avatar of dudup

ASKER

Thanks Geert :) It works ... You are the best, especially for TVirtualStringTree !
Avatar of dudup

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 :)