Solved

Drag and drop between two TreeViews...

Posted on 2000-05-12
9
328 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:knowlton
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by: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: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: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:knowlton
ID: 2883288
What about 2 TFileListBox components?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

765 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