[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

TreeView drag&drop

Hi,

I need a TreeView which works like Internet Explorer's "Favorites": when dragging a node over a narrow area between two nodes, it displays a line to show that the dragged node can be dropped to the same level. When moving the mouse pointer further inside the display rectangle of a node, it becomes selected to show, that the dragged node would become the subnode of the selected one.

I checked TTreeView's source, yet I can't see how to do it. :(
Any idea?

Regards,
CC
0
cc042297
Asked:
cc042297
  • 2
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
mmm...so something like this.....

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    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 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 = nil) then
    begin
      EndDrag(False);
      Exit;
    end;
    MoveNode(TargetNode,SourceNode);
    SourceNode.Free;
    TargetNode.Selected := True;
    TargetNode.Expand(True);
  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.


object Form1: TForm1
  Left = 192
  Top = 107
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 84
    Top = 26
    Width = 195
    Height = 257
    DragMode = dmAutomatic
    Indent = 19
    TabOrder = 0
    OnDragDrop = TreeView1DragDrop
    OnDragOver = TreeView1DragOver
    Items.Data = {
      030000001A0000000000000000000000FFFFFFFFFFFFFFFF0000000001000000
      01311A0000000000000000000000FFFFFFFFFFFFFFFF00000000030000000132
      1A0000000000000000000000FFFFFFFFFFFFFFFF000000000000000001331A00
      00000000000000000000FFFFFFFFFFFFFFFF000000000000000001341A000000
      0000000000000000FFFFFFFFFFFFFFFF000000000000000001351A0000000000
      000000000000FFFFFFFFFFFFFFFF000000000200000001361A00000000000000
      00000000FFFFFFFFFFFFFFFF000000000000000001371A000000000000000000
      0000FFFFFFFFFFFFFFFF000000000000000001381A0000000000000000000000
      FFFFFFFFFFFFFFFF000000000100000001391B0000000000000000000000FFFF
      FFFFFFFFFFFF0000000000000000023130}
  end
end
0
 
cc042297Author Commented:
Well, if I'am not missing something, your code moves the selected node to a new position, but I can't see anything about my real problem: how to get TreeView to draw a line between two nodes, when dragging? Any idea about that? :)



0
 
kretzschmarCommented:
you may take a look to mike lischke's virtualTreeView.

there is such bahaviour (and much more) already implemented

see
http://www.soft-gems.net/VirtualTreeview/VT.php

meikl ;-)
0
 
cc042297Author Commented:
Thanx Meikl ! :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now