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

Drag'n'Drop nodes inside TreeView

By Zarko Gajic, About.com

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

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. 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.