AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

DragAndDrop zwischen 2 TreeView

Ein Thema von EccoBravo · begonnen am 30. Nov 2005 · letzter Beitrag vom 14. Feb 2006
Antwort Antwort
Benutzerbild von EccoBravo
EccoBravo

Registriert seit: 19. Okt 2004
Ort: Neuruppin
524 Beiträge
 
Delphi 2007 Architect
 
#1

DragAndDrop zwischen 2 TreeView

  Alt 30. Nov 2005, 16:18
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.
  Mit Zitat antworten Zitat
Deadinpac

Registriert seit: 8. Aug 2003
Ort: Oberreute
74 Beiträge
 
Delphi 2007 Enterprise
 
#2

Re: DragAndDrop zwischen 2 TreeView

  Alt 30. Nov 2005, 16:59
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
Sven Grimm
  Mit Zitat antworten Zitat
Benutzerbild von EccoBravo
EccoBravo

Registriert seit: 19. Okt 2004
Ort: Neuruppin
524 Beiträge
 
Delphi 2007 Architect
 
#3

Danke das war eine sehr gute Antwort.

  Alt 30. Nov 2005, 17:42
Danke das war eine sehr gute Antwort.

Herzliche Winter- und Adventsgrüße

E. B.
  Mit Zitat antworten Zitat
Dust Signs

Registriert seit: 28. Dez 2004
Ort: Salzburg
379 Beiträge
 
#4

Re: DragAndDrop zwischen 2 TreeView

  Alt 30. Nov 2005, 17:46
Wie wär's eventuell noch mit Delphi-Tags zwecks Lesbarkeit

Dust Signs
(aka AXMD in der EE)
Die Nummer, die Sie gewählt haben, ist imaginär. Bitte drehen Sie Ihr Telefon um 90° und versuchen Sie es erneut.
  Mit Zitat antworten Zitat
Deadinpac

Registriert seit: 8. Aug 2003
Ort: Oberreute
74 Beiträge
 
Delphi 2007 Enterprise
 
#5

Re: DragAndDrop zwischen 2 TreeView

  Alt 30. Nov 2005, 17:46
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]
Sven Grimm
  Mit Zitat antworten Zitat
madina

Registriert seit: 1. Nov 2005
153 Beiträge
 
#6

Re: DragAndDrop zwischen 2 TreeView

  Alt 14. Feb 2006, 10:08
Hallo,

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

mfg
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:21 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