Solved

Drag and drop between two TreeViews...

Posted on 2000-05-12
9
339 Views
Last Modified: 2010-04-04
Sorry only 50 points, but I'm getting low on points again.  So, if you're feeling chartitable...

I have two TreeViews side by side.  I want to duplicate some of the Windows Epxlorer type functionality by enabling "drag-and-drop"  (both ways) between the 2 TreeViews.

I'm looking for some sample code that will work with a form plus 2 TreeView components dropped on it and away I go.

Thanks!
0
Comment
Question by:Tom Knowlton
[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
9 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 50 total points
ID: 2806418
Knowlton,

Add the code below to the respective events of both TTreeView components:

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
      Accept := Source is TTreeView;
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
      NewNode, SrcNode, DestNode : TTreeNode;
begin
      SrcNode := TTreeView(Source).Selected;
  DestNode := TTreeView(Sender).GetNodeAt(X, Y);

  if DestNode = nil then
        NewNode := TTreeView(Sender).Items.Add(nil, SrcNode.Text)
  else
        NewNode := TTreeView(Sender).Items.AddChild(DestNode, SrcNode.text);
   // If you want to "MOVE" the node instead of Copying it, add the code below
   // SrcNode.Delete;
end;


Also remember to set both TTreeView's DragMode property to dmAutomatic.

Yours,

Alex
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2806550
if you want copy or move the node and all his childnode. use my code.  :)

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TTreeView;
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
  procedure CopyChildNode(ASNode:TTreeNode;var ADNode:TTreeNode);
  var
    i:integer;
    CNode:TTreeNode;
  begin
    for i:=0 to ASNode.Count-1 do
    begin
      CNode:=TTreeView(Sender).Items.AddChild(ADNode,ASNode.Item[i].Text);
      if ASNode.Item[i].HasChildren then
        CopyChildNode(ASNode.Item[i],CNode);
    end;
  end;

var
  NNode, SNode, DNode : TTreeNode;
begin
  SNode := TTreeView(Source).Selected;
  DNode := TTreeView(Sender).GetNodeAt(X, Y);

  if DNode = nil then
   NNode := TTreeView(Sender).Items.Add(nil, SNode.Text)
  else
   NNode := TTreeView(Sender).Items.AddChild(DNode, SNode.text);
  if SNode.HasChildren then CopyChildNode(SNode,NNode);
  //SNode.Delete;
end;
0
 
LVL 15

Expert Comment

by:simonet
ID: 2806849
>use my code.  :)

As if your code wasn't 85% percent copied. You barely changed the names of the variables.
0
Independent Software Vendors: 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!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 2807436
simonet:

Works great!

Thanks.
0
 
LVL 15

Expert Comment

by:simonet
ID: 2807461
Glad to know, knowlton.

Funny thing is I never got an e-mail notification that you accepted my answer. Is it me or has EE been very weird in the last 2 days?

Alex
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 2808376
I suspect EE, 'cause I answered your question.
0
 
LVL 3

Expert Comment

by:darinw
ID: 2808644
Reviewing question.

darinw
Customer Service
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 2808916
What I meant to say was, you answered my question and I accepted.

Yes, the points belong to simonet.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 2883288
What about 2 TFileListBox components?
0

Featured Post

Independent Software Vendors: 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

688 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