我需要向TVirtualStringTree插入一些新节点。我使用带有UserData参数的InsertNode方法。但树无法显示任何数据。如何获取GetText / FreeNode事件的用户数据?
TMyData = record
Name: string;
size: Integer;
end;
PMyData = ^TMyData;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
VirtualStringTree1.NodeDataSize := SizeOf(TMyData);
Randomize;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
Data: PMyData;
Node: PVirtualNode;
begin
New(Data);
Data.Name := DateTimeToStr(now);
Data.size := Random(100);
Node := VirtualStringTree1.InsertNode(nil, amInsertAfter, Data);
end;
procedure TForm2.VirtualStringTree1FreeNode(Sender: TBaseVirtualTree; Node:
PVirtualNode);
var
Data: PMyData;
begin
Data := Sender.GetNodeData(Node);
Data.Name := '';
end;
procedure TForm2.VirtualStringTree1GetText(Sender: TBaseVirtualTree; Node:
PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText:
WideString);
var
Data: PMyData;
begin
Data := Sender.GetNodeData(Node);
case Column of
0: CellText := Data.Name;
1: CellText := IntToStr(Data.size);
end;
end;
答案 0 :(得分:6)
在树中插入用户数据时,指针存储在节点的内部记录中。 GetNodeData
不会返回您传入的指针,它是指向存储指针的内存的指针。因此,您需要取消引用它:
Data := PMyData(Sender.GetNodeData(Node)^);