unit frmVstCopyTest;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, VirtualTrees,
Vcl.StdCtrls;
type
ITreeNode =
interface
['
{E64AAC69-EA11-4E5B-BB9C-13A6C37FB8BC}']
function GetText:
string;
end;
TTreeNode =
class(TInterfacedObject, ITreeNode)
strict private
FText:
string;
public
constructor Create(
const ANumber: Integer);
function GetText:
string;
end;
TForm2 =
class(TForm)
vstMain: TVirtualStringTree;
vstSub: TVirtualStringTree;
procedure FormCreate(Sender: TObject);
procedure vstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText:
string);
procedure vstMainFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
procedure vstMainNodeCopying(Sender: TBaseVirtualTree; Node, Target: PVirtualNode;
var Allowed: Boolean);
private
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
var
NodeData: ITreeNode;
NewNode: PVirtualNode;
I, J: Integer;
begin
for I := 1
to 3
do
begin
NodeData := TTreeNode.Create(I);
NewNode := vstMain.AddChild(
nil, NodeData);
for J := 1
to 10
do
begin
NodeData := TTreeNode.Create(J);
vstMain.AddChild(NewNode, NodeData);
end;
end;
end;
{ TTreeNode }
constructor TTreeNode.Create(
const ANumber: Integer);
begin
FText := ANumber.ToString;
end;
function TTreeNode.GetText:
string;
begin
Result := '
Hallo Welt ' + FText;
end;
procedure TForm2.vstMainFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
var
NewNode: PVirtualNode;
ChildNode: PVirtualNode;
begin
vstSub.Clear;
// Das hier funktioniert NICHT:
// NewNode := Sender.CopyTo(Node, vstSub, TVTNodeAttachMode.amAddChildLast, False);
// das manuelle hinzufügen funktioniert, aber das kann es ja wohl nicht sein?
NewNode := vstSub.AddChild(
nil, Node.GetData<ITreeNode>);
ChildNode := Node.FirstChild;
while ChildNode <>
nil do
begin
vstSub.AddChild(NewNode, ChildNode.GetData<ITreeNode>);
ChildNode := ChildNode.NextSibling;
end;
vstSub.FullExpand();
end;
//Das Event hier wird zwar aufgerufen, aber GetData vom Node liefert nil -> Hä? Wie jetzt?
procedure TForm2.vstMainNodeCopying(Sender: TBaseVirtualTree; Node, Target: PVirtualNode;
var Allowed: Boolean);
var
NodeData: ITreeNode;
begin
NodeData := Node.GetData<ITreeNode>;
if Assigned(NodeData)
then
Target.SetData<ITreeNode>(NodeData);
end;
procedure TForm2.vstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText:
string);
var
NodeData: ITreeNode;
begin
NodeData := Node.GetData<ITreeNode>;
if Assigned(NodeData)
then
CellText := NodeData.GetText
else
CellText := '
Kein Text';
end;
end.