Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi DragAndDrop zwischen 2 TreeView (https://www.delphipraxis.net/58051-draganddrop-zwischen-2-treeview.html)

EccoBravo 30. Nov 2005 16:18


DragAndDrop zwischen 2 TreeView
 
Hallo,

als ob es nicht genug Fragen über TreeView geben würde, hier noch eine:

Stellt Euch vor, ein ganz normales WINDOWS-Programm, ich habe zwei TreeViews, den ersten mit m Einträgen (m Items) den 2. daneben mit n Einträgen (n Items).
Und ich will mit DragAndDrop ein bestimmtes Item aus den 2. TreeView unter ein bestimmtes Item des 1. TreeViews kopieren.
Ich sitze davor und habe nach x Programmierjahren keine Ahnung, besonders nicht von TreeViews?!?!?

Kann mir jemand erklären, wie das geht??

Vielen Dank

E. B.

Deadinpac 30. Nov 2005 16:59

Re: DragAndDrop zwischen 2 TreeView
 
Moin,

Vllt kann dir das Weiterhelfen

Zitat:

Delphi-Quellcode:
Drag and Drop


First a clarification, Drag and Drop here refers to drag and drop within the TreeView, or from the TreeView to another control within the same application. This tutorial does not cover drag and drop between applications, eg to/from Explorer. That has more to do with OLE than it does TreeViews.

This example is based on example 10 and there are thus file and folder nodes as well as a root node. Take a look at the "rules" for this TreeView (example 10). These rules must be kept when nodes are dragged.

The OnDragOver event is called when something (on this case a TreeNode) is dragged over a control (a TTreeView). You must indicate if the dragged item may be dropped at the current position.


   procedure TForm1.tv_eg5DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
   begin
   /////////////////////////////////////////
   // Decide if drag-drop is to be allowed
   /////////////////////////////////////////

      Accept := false;

         {Only accept drag and drop from a TTreeView}
      if(  Sender is TTreeView ) then
            {Only accept from self}
         if(  TTreeView(Sender) = tv_eg5  ) then
            Accept := true;
   end;

   1. Assume fail
   2. If the source is a TreeView
   3. If dragging internally ie tv_eg5 to ev_eg5
   4. Allow drag-drop


The OnDragDrop event is called once an item is dropped on the TTreeView. This is a rather long function so a outline of the procedure will help clarify what exactly it does.

   1. Get the target node (the node that the item was dropped on)
   2. Get an alias for the source node. This makes the source easier to read
   3. Make sure the Target is a valid node
   4. Can the target node accept the source node
         1. Cant drop onto self, or drop onto immediate parent
         2. May not drag the root
         3. Cant drop a parent onto a child
         4. May not drop if an item with the same names as the source already exists
   5. Check the rules - IsNodeAllowed
   6. Copy the node
   7. Delete old node (Copy + Delete = move)
   8. Display node in its new position


   procedure TForm1.tv_eg5DragDrop(Sender, Source: TObject; X, Y: Integer);
   var
      TargetNode : TTreeNode;
      SourceNode : TTreeNode;
   begin
   /////////////////////////////////////////
   // Somthing has just been droped
   /////////////////////////////////////////

      with tv_eg5 do
      begin
            {Get the node the item was dropped on}
         TargetNode := GetNodeAt(  X, Y );
            {Just to make things a bit easier}
         SourceNode := Selected;


            {Make sure somthing was droped onto}
         if(  TargetNode = nil ) then
         begin
            EndDrag(  false );
            Exit;
         end;

            {Dropping onto self or onto parent?}
         if(  (TargetNode = Selected) or
              (TargetNode = Selected.Parent)
           ) then
         begin
            MessageBeep(  MB_ICONEXCLAMATION );
            ShowMessage(  'Destination node is the same as the source node' );
            EndDrag(  false );
            Exit;
         end;

            {No drag-drop of the root allowed}
         if(  SourceNode.Level = 0  ) then
         begin
            MessageBeep(  MB_ICONEXCLAMATION );
            ShowMessage(  'Cant drag/drop the root' );
            EndDrag(  false );
            Exit;
         end;

            {Can't drop a parent onto a child}
         if(   IsAParentNode(  Selected, TargetNode )  ) then
         begin
            MessageBeep(  MB_ICONEXCLAMATION );
            ShowMessage(  'Cant drop parent onto child' );
            EndDrag(  false );
            Exit;
         end;

            {Does a node with this name exists as a child of TargetNde}
         if(   IsDuplicateName(  TargetNode.GetFirstChild, SourceNode.Text, true )  ) then
         begin
            MessageBeep(  MB_ICONEXCLAMATION );
            ShowMessage(  'A node with this name already exists' );
            EndDrag(  false );
            Exit;
         end;

         //////////////////////////////////////////////////////////////
         // Nothing differant up to here. Just the normal drag and
         //   drop checking. Now the code to make sure that enforce
         //   "the rules". Eg books may contain no sub-nodes
         //////////////////////////////////////////////////////////////
   
            {Use the IsNodeAllowed function to test if the node
              may be dropped here}
         if(  not IsNodeAllowed(   TargetNode,
                                   GetNodeType(  SourceNode )
                                 )
            ) then
         begin
            MessageBeep(  -1  );
            ShowMessage(  'You cant drop this type of node here!' );
            EndDrag(  false );
            Exit;
         end;


            {Drag drop was valid so move the nodes}
         MoveTreeNode(  tv_eg5, SourceNode, TargetNode );

            {Delete the old node}
         SourceNode.Delete;


            {Show the nodes that were just moved}
         TargetNode.Expand(  true );
      end;
   end;

Drag and Drop between a TreeView and its linked ListView (eg 12) is a bit more difficult. Although not that much more...
Use a function that works similarly to the OnDragDrop function above, but TargetNode and SourceNode are passed as parameters

Then...
TreeView to ListView    Get the ListItem the TreeNode was dropped onto. Get the TreeNode that this ListItem is linked to and use it as the TargetNode.
ListView.TreeView    Get dragged TreeItem, get linked TreeNode use this as SourceNode
ListView to ListView    Get source and target ListItems, get linked Source and Target TreeNodes. Call function

I've not actually implemented this, but with the examples in this tutorial you should be able to get this working without too much trouble.

Hab ich Hier gefunden

[Edit] Jeder wunsch ist mir recht[/edit]
MFG
Sven

EccoBravo 30. Nov 2005 17:42

Danke das war eine sehr gute Antwort.
 
Danke das war eine sehr gute Antwort.

Herzliche Winter- und Adventsgrüße

E. B.

Dust Signs 30. Nov 2005 17:46

Re: DragAndDrop zwischen 2 TreeView
 
Wie wär's eventuell noch mit Delphi-Tags zwecks Lesbarkeit ;)

Dust Signs

Deadinpac 30. Nov 2005 17:46

Re: DragAndDrop zwischen 2 TreeView
 
Moin,

Mir ist noch eigefallen es gibt auch noch die Kompo Virtual Treeview

habs sie bei mir kurz reingemacht und die demos angeguckt und da kann man auch drag and drop machen ohne Quelltext.

Und hat auch viel mehr möglichkeiten als das normale Treeview

Virtual Treeview

MFG
Sven


[edit=Matze]Link aktualisiert. MfG, Matze[/edit]

madina 14. Feb 2006 10:08

Re: DragAndDrop zwischen 2 TreeView
 
Hallo,

wie konnte man vom Appl1.TreeView zu Appl2.TreeView Knoten verschieben, wenn (Appl1=Appl2) ist aber 2 mal aufgerufen wurde ??

mfg


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:28 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz