Solved

TreeView drag&drop

Posted on 2004-03-31
4
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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