[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Drag and drop between two TreeViews...

Posted on 2000-05-12
9
Medium Priority
?
368 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 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