![]() |
Treeview Zweig woanders kopieren
Ich hab hier grad ein etwas verzwicktes Problem.
Und zwar muss ich einen Treeview Zweig, der selbst auch wieder Childnodes und ChildChildnodes enthalten kann, als Kopie woanders in die Treeview kopieren. Ich kann zwar ein der Zielstelle mittels
Delphi-Quellcode:
eine Kopie woanders erstellen, jedoch mit einem Haken: Die Subnodes von MyCopyNode werden nicht mit erstellt :wall: Ausserdem hängt an jedem Node noch ein Pointer der eine Datenstruktur beinhaltet (TNodeDetails). Die soll natürlich auch als Kopie angelegt werden. Weil aber beim eingefügten Node nur der Pointer kopiert wird, verweist der natürlich in beiden Nodes auf das gleiche Objekt :wall:
NewNode := Treeview1.Items.AddChild(treeview1.Selected,'Blubb') ;
NewNode.Assign(MyCopyNode); // MyCopyNode soll geclont werden Treeview1.Selected := NewNode ; Kurz: Was ist such ist sowas wie MyCopyNode.MoveTo... Eben nur als Kopie und nicht als Verschieben. |
Re: Treeview Zweig woanders kopieren
Ich habs dann wirklich alles händisch machen müssen, sprich, den Zweig rekursiv durchlaufen und an der gewünschten Stelle neu erstellen. Zu den Objekten, die an jedem Knoten klebten, habe ich ebenfalls jeweils natürlich eine neue Instanz erzeugen müssen, und mittels einer selbst geschriebenen Assign Methode in meiner Klasse konnte ich mir dann eine Kopie der Struktur erstellen.
Wenn ich heut abend oder morgen abend dazu komm werd ich den Code mal ausführlich als Komponente veröffentlichen, also quasi eine Treeview erstellen mit der Zusatzmethode CopyTo (als Pendant zu MoveTo). Wird ja dann vielleicht was für die CodeLib. |
Re: Treeview Zweig woanders kopieren
Hallo
Ich habe mal die CopySubtree Funktion von P. Below verwendet.
Delphi-Quellcode:
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; |
Re: Treeview Zweig woanders kopieren
Naja, viel anders hab ichs ja auch nicht. nur im Assign des Knoten muss natürlich auch eine neue Datenstruktur erstellt werden. Aber das ist ja leicht anpassbar.
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:43 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz