Solved

TreeView drag&drop

Posted on 2004-03-31
4
248 Views
Last Modified: 2010-04-05
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
Comment
Question by:cc042297
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10728073
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
 

Author Comment

by:cc042297
ID: 10728158
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 10729673
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
 

Author Comment

by:cc042297
ID: 10760317
Thanx Meikl ! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 199
Magic Software info 18 125
How to debug For loops? 3 45
tvirtualstringtree freeze when load too manny images 10 52
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now