Solved

TreeView drag&drop

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

16 Experts available now in Live!

Get 1:1 Help Now