在 TVirtualStringTree 中插入带有 UserData 参数的节点
我需要向 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;
I need to insert some new nodes to TVirtualStringTree. I use the InsertNode method with a UserData param. But the tree can't show any data. How can I get the userdata on GetText/FreeNode event?
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在树中插入用户数据时,您的指针将存储在节点的内部记录中。
GetNodeData
不会返回您传入的指针,它是指向存储指针的内存的指针。因此您需要取消引用它:When you insert user data in the tree your pointer is stored in the node's internal record.
GetNodeData
doesn't return the pointer which you passed in, it's the pointer to memory where your pointer is stored. Therefore you need to dereference it: