Tasomia
asked on
Copy & Paste in a TreeView ?
Has anybody an example how to realize
copy & paste in a TTreeView?
Multiselect = false but the function should also copy
the children of a node.
Thanks
tas
copy & paste in a TTreeView?
Multiselect = false but the function should also copy
the children of a node.
Thanks
tas
listening
standard TreeView not support copy/paste tree nodes.
But you can write component with the feature.
But you can write component with the feature.
ASKER
But how ?
if you would raise the points,
then i would post a sample :-))
then i would post a sample :-))
ASKER
Ok, now you get 130 points!
hu, well ok,
tell me which delphi-version u use
tell me which delphi-version u use
Example:
TCPTreeView = class(TTreeView)
private
procedure WMCopy(var Msg : TMessage); message WM_COPY;
procedure WMPaste(var Msg : TMessage); message WM_PASTE;
protected
procedure AddNodes(ATree : TTreeView; ASrc, ADst: TTreeNode);
public
procedure DoCopy;
procedure DoPaste;
End;
-------------------------- ----
procedure TCPTreeView.DoCopy;
begin
Perform(WM_COPY, 0, 0);
end;
procedure TCPTreeView.DoPaste;
begin
Perform(WM_PASTE, 0, 0);
end;
procedure TCPTreeView.AddNodes(ATree : TTreeView; ASrc, ADst : TTreeNode);
Var
NextSrc, NextDst : TTreeNode;
Begin
NextDst := ATree.Items.AddChild(ADst, '');
NextDst.Assign(ASrc);
NextSrc := ASrc.GetFirstChild;
while Assigned(NextSrc) Do Begin
AddNodes(ATree, NextSrc, NextDst);
NextSrc := ASrc.GetNextChild(NextSrc) ;
End;
End;
procedure TCPTreeView.WMCopy(var Msg: TMessage);
Var
T : TTreeView;
begin
If not Assigned(Selected) Then Exit;
T := TTreeView.Create(nil);
Try
T.Visible := False;
T.Parent := Self;
AddNodes(T, Selected, nil);
ClipBoard.SetComponent(T);
finally
T.Free;
end;
end;
procedure TCPTreeView.WMPaste(var Msg: TMessage);
Var
A : TComponent;
T : TTreeView;
begin
Try
A := ClipBoard.GetComponent(Sel f, Self);// as TCPTreeView;
T := A as TTreeView;
AddNodes(Self, T.Items[0], Selected);
finally
T.Free;
end;
end;
Regards.
TCPTreeView = class(TTreeView)
private
procedure WMCopy(var Msg : TMessage); message WM_COPY;
procedure WMPaste(var Msg : TMessage); message WM_PASTE;
protected
procedure AddNodes(ATree : TTreeView; ASrc, ADst: TTreeNode);
public
procedure DoCopy;
procedure DoPaste;
End;
--------------------------
procedure TCPTreeView.DoCopy;
begin
Perform(WM_COPY, 0, 0);
end;
procedure TCPTreeView.DoPaste;
begin
Perform(WM_PASTE, 0, 0);
end;
procedure TCPTreeView.AddNodes(ATree
Var
NextSrc, NextDst : TTreeNode;
Begin
NextDst := ATree.Items.AddChild(ADst,
NextDst.Assign(ASrc);
NextSrc := ASrc.GetFirstChild;
while Assigned(NextSrc) Do Begin
AddNodes(ATree, NextSrc, NextDst);
NextSrc := ASrc.GetNextChild(NextSrc)
End;
End;
procedure TCPTreeView.WMCopy(var Msg: TMessage);
Var
T : TTreeView;
begin
If not Assigned(Selected) Then Exit;
T := TTreeView.Create(nil);
Try
T.Visible := False;
T.Parent := Self;
AddNodes(T, Selected, nil);
ClipBoard.SetComponent(T);
finally
T.Free;
end;
end;
procedure TCPTreeView.WMPaste(var Msg: TMessage);
Var
A : TComponent;
T : TTreeView;
begin
Try
A := ClipBoard.GetComponent(Sel
T := A as TTreeView;
AddNodes(Self, T.Items[0], Selected);
finally
T.Free;
end;
end;
Regards.
you should add some code to keyboard handler to check for ^C/^V combination.
seems someone had waited too
good work, slavak :-)
good work, slavak :-)
ASKER
Hi Slavak,
Thanks for your great code!
I had to add
RegisterClasses([TTreeView ]);
in order to get it work.
Now as it works for the standard TTreeView
I wanted it to work with TTreeNT.
I adapted the Code but when I run DoPaste I get an error
"the index of the list exceeds the maximum (0).
error raises here:
function TTreeNodeList.Get(Index: Integer): TTreeNTNode;
begin
if (Index < 0) or (Index >= FCount) then
{$ifdef DFS_COMPILER_3_UP}
Error(SListIndexError, Index);
...
Does anybody have an idea why ?
tas
BTW: Slavak, the points are yours but I would be glad if somebody could help me with the TTreeNT problem.
Thanks for your great code!
I had to add
RegisterClasses([TTreeView
in order to get it work.
Now as it works for the standard TTreeView
I wanted it to work with TTreeNT.
I adapted the Code but when I run DoPaste I get an error
"the index of the list exceeds the maximum (0).
error raises here:
function TTreeNodeList.Get(Index: Integer): TTreeNTNode;
begin
if (Index < 0) or (Index >= FCount) then
{$ifdef DFS_COMPILER_3_UP}
Error(SListIndexError, Index);
...
Does anybody have an idea why ?
tas
BTW: Slavak, the points are yours but I would be glad if somebody could help me with the TTreeNT problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I still can't get it work. The selection is not empty!
What's wrong ?
What's wrong ?
ASKER
I still can't get it work. The selection is not empty!
What's wrong ?
What's wrong ?
I'm not using TreeNT component and cannot do it myself, but it should be simple debugging check.
Place breakpoint in the function, and you will see Index value that make a problem.
Place breakpoint in the function, and you will see Index value that make a problem.
ASKER
The problem is, that after
T := A as TMYTreeNT; // My TreeNT Component
T.Items.Count is always = 0
T := A as TMYTreeNT; // My TreeNT Component
T.Items.Count is always = 0
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
accept Slavak's comment as answer
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Thanks,
geobul
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
accept Slavak's comment as answer
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Thanks,
geobul
EE Cleanup Volunteer