Board index » delphi » Combining Treeviews

Combining Treeviews


2003-07-23 04:32:00 PM
delphi136
Hi,
using D7. I want to combine the nodes of 2 Treeviews.
So the first Treeview should be a subnode of the second Treeview.
I'm missing a function where I can copy the Nodes.
Something like
Treeview1.items.CopyNodes(aNode,TreeView2.items)
where aNode is a Subnode from Treeview1 where the nodes
of TreeView2 should be inserted. (But Copy not Move!)
Is there any way or have I to code this function?
Thanks in Advance
 
 

Re:Combining Treeviews

In article <3f1e4800$XXXX@XXXXX.COM>, Holger Niemann writes:
Quote
using D7. I want to combine the nodes of 2 Treeviews.
So the first Treeview should be a subnode of the second Treeview.

I'm missing a function where I can copy the Nodes.

Something like

Treeview1.items.CopyNodes(aNode,TreeView2.items)

where aNode is a Subnode from Treeview1 where the nodes
of TreeView2 should be inserted. (But Copy not Move!)

Is there any way or have I to code this function?
You have to code it. If you use items.Assign you would *replace* all
the nodes in treeview1 with those of treeview2. I see no way other than
to create new nodes in treeview1 and Assign them the corresponding
nodes in treeview2.
I found this snippet in my collection:
Type
{: Callback to use to copy the data of a treenode when the
node itself is copied by CopySubtree.
@param oldnode is the old node
@param newnode is the new node
@Desc Use a callback of this type to implement your own algorithm
for a node copy. The default just uses the Assign method, which
produces a shallow copy of the nodes Data property. }
TCopyDataProc = Procedure( oldnode, newnode: TTreenode );
{: The default operation is to do a shallow copy of the node, via
Assign. }
Procedure DefaultCopyDataProc( oldnode, newnode: TTreenode );
Begin
newnode.assign( oldnode );
End;
{-- CopySubtree
-------------------------------------------------------}
{: Copies the source node with all child nodes to the target treeview.
@Param sourcenode is the node to copy
@Param target is the treeview to insert the copied nodes into
@Param targetnode is the node to insert the copy under, can be nil to
make the copy a top-level node.
@Param CopyProc is the (optional) callback to use to copy a node.
If Nil is passed for this parameter theDefaultCopyDataProc will be
used.
@Precondition sourcenode <>nil, target <>nil, targetnode is either
nil or a node of target
@Raises Exception if targetnode happens to be in the subtree rooted in
sourcenode. Handling that special case is rather complicated, so we
simply refuse to do it at the moment.
}{ Created 2003-04-09 by P. Below
-----------------------------------------------------------------------
}
Procedure CopySubtree( sourcenode: TTreenode; target: TTreeview;
targetnode: TTreenode; CopyProc: TCopyDataProc = nil );
Var
anchor: TTreenode;
child: TTreenode;
Begin { CopySubtree }
Assert( Assigned( sourcenode ),
'CopySubtree:sourcenode cannot be nil' );
Assert( Assigned( target ),
'CopySubtree: target treeview cannot be nil' );
Assert((targetnode = nil) or (targetnode.TreeView = target ),
'CopySubtree: targetnode has to be a node in the target
treeview.' );
If (sourcenode.TreeView = target) and
(targetnode.HasAsParent( sourcenode ) or (sourcenode =
targetnode))
Then
raise Exception.Create(
'CopySubtree cannot copy a subtree to one of the '+
'subtrees nodes.');
If not Assigned( CopyProc ) Then
CopyProc := DefaultCopyDataProc;
anchor := target.Items.AddChild( targetnode, sourcenode.Text );
CopyProc( sourcenode, anchor );
child := sourcenode.getFirstChild;
While Assigned( child ) Do Begin
CopySubtree( child, target, anchor, CopyProc );
child := child.getNextSibling;
End; { While }
End; { CopySubtree }
procedure TForm1.Button1Click(Sender: TObject);
begin
if assigned(treeview1.selected) then
CopySubtree( treeview1.selected, treeview2, nil );
end;
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be