1. Home
  2. Computing & Technology
  3. Delphi Programming

Drag'n'Drop nodes inside TreeView

By , About.com Guide

The following code uses GetNodeAt to add a dragged node as a child of the node under then mouse when it is dropped.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.TreeView1DragDrop
(Sender, Source: TObject; X, Y: Integer) ;
var
   AnItem: TTreeNode;
   AttachMode: TNodeAttachMode;
   HT: THitTests;
begin
   if TreeView1.Selected = nilthen Exit;
   HT := TreeView1.GetHitTestInfoAt(X, Y) ;
   AnItem := TreeView1.GetNodeAt(X, Y) ;
   if (HT -
     [htOnItem, htOnIcon, htNowhere, htOnIndent]
<> HT) then
   begin
     if (htOnItem in HT) or
        (htOnIcon in HT) then
         AttachMode := naAddChild
     else if htNowhere in HT then
AttachMode := naAdd
     else if htOnIndent in HT then
AttachMode := naInsert;
     TreeView1.Selected.
MoveTo(AnItem, AttachMode) ;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Get Default Printer Name
« Show *any* graphics as Glyph on a SpeedButton

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2001 Delphi Tips
  7. Drag'n'Drop nodes inside TreeView

©2009 About.com, a part of The New York Times Company.

All rights reserved.