whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

69 Visitors Online


 
...Drag and Drop Items within TTreeView?
Autor: Igor Savic
[ Print tip ]  

Tip Rating (26):  
     


unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  
TForm1 = class(TForm)
    TreeView1: TTreeView;
    Label1: TLabel;
    procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
    
{ Private declarations }
    
Procedure MoveNode(TargetNode, SourceNode : TTreeNode);
  public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.dfm}

//
// Procedure which will move node and its subnodes
//
Procedure TForm1.MoveNode(TargetNode, SourceNode : TTreeNode);
var
  
nodeTmp : TTreeNode;
  i : Integer;
begin
  with 
TreeView1 do
  begin
    
nodeTmp := Items.AddChild(TargetNode,SourceNode.Text);
    for i := 0 to SourceNode.Count -1 do
    begin
      
MoveNode(nodeTmp,SourceNode.Item[i]);
    end;
  end;
end;


procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  
TargetNode, SourceNode : TTreeNode;
begin
  with 
TreeView1 do
  begin
    
TargetNode := GetNodeAt(X,Y); // Get target node
    
SourceNode := Selected;
    if (TargetNode = nilthen
    begin
      
EndDrag(False);
      Exit;
    end;
    MoveNode(TargetNode,SourceNode);
    SourceNode.Free;
  end;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if 
(Sender = TreeView1) then // If TRUE than accept the draged item
  
begin
    
Accept := True;
  end;
end;

end.


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners