开发者

InsertNode with UserData param in TVirtualStringTree

开发者 https://www.devze.com 2023-03-07 03:43 出处:网络
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?

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:
    PVirtualNod开发者_StackOverflow社区e; 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;


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:

Data := PMyData(Sender.GetNodeData(Node)^);
0

精彩评论

暂无评论...
验证码 换一张
取 消