Solved

Drag and drop between two TreeViews...

Posted on 2000-05-12
9
318 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
 
LVL 5

Author Comment

by:knowlton
ID: 2807436
simonet:

Works great!

Thanks.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now