We help IT Professionals succeed at work.

TVirtualStringTree copy between two trees

dudup
dudup asked
on
1,280 Views
Last Modified: 2010-04-21
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.
Comment
Watch Question

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

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

Author

Commented:
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.
Oracle dba
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
BtoDummy has to DummytoB
but I think you got that one ?

Author

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

Author

Commented:
Hi Geert,

Your code is interesting, I will try that.

Author

Commented:
If you define:

VirtualStringTreeA: TVirtualStringTree;

Dont you need to CREATE it in the FormCreate?
Top Expert 2004

Commented:
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
it was created with the Form Designer
drop a VirtualStringTree on the form 3x times
and rename them to A, B and Dummy

Author

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

Author

Commented:
In my case, I define the dummy_tree manually as public variable of TForm1:

dummy_tree: TVirtualStringTree;

..... in the FormCreate:

dummy_tree := TVirtualStringTree.nil;

Author

Commented:
Fixed the error:

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

Author

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

Author

Commented:
Geert, I think thats because the "TargetNode" in the CopyNodesToTree is not used at all....

And I cannot figure out where to put it.
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
.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



Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
especially don't forget the NodeDataSize ...

Author

Commented:
Found it !

---> CopyNode(SourceNode, TargetNode, False, True);
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
it did need a little tweaking :)

Author

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

Author

Commented:
There was a bug in the code, but it is easy to fix :)
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
uhm, i don't think so, but i'm not argueing :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.