Solved

Drag and drop between two TreeViews...

Posted on 2000-05-12
9
332 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
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
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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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