Solved

TreeView drag&drop

Posted on 2004-03-31
4
249 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

9 Experts available now in Live!

Get 1:1 Help Now