How to assign data to node of VirtualStringTree in InitNode event

后端 未结 1 703
不知归路
不知归路 2021-01-16 10:06

I am not sure how to assign data to a node in a VirtualStringTree. I\'m need to assign a pointer to a record object to the Node\'s Data property in the tree control\'s InitN

相关标签:
1条回答
  • 2021-01-16 10:34

    Do not read or write Node.Data directly. The data you need won't necessarily be exactly at the address of that field. (The tree control has a mechanism for allowing descendants to reserve additional data for themselves.) Instead, call Sender.GetNodeData.

    var
      NodeData: PDiagData;
    begin
      NodeData := Sender.GetNodeData(Node);
      NodeData^ := TDiagData(FDiagDataList.Items[c]);
    end;
    

    Your code fails because Node.Data has type record; you cannot dereference it with ^. In the simple case, the value returned by GetNodeData will be equal to the address of that field (i.e., GetNodeData(Node) = @Node.Data). But don't assume all cases are simple. As I said, tree-control descendants can reserve data space of their own, so you're sharing that space with code that's outside your control, and it's up to the tree control to manage which data space is yours. Always call GetNodeData.


    Furthermore, you're confused about your data types. You say FDiagDataList is a TObjectList, but you're clearly storing something in it that isn't a descendant of TObject. When you're not using objects, don't use TObjectList. If you're using a version of Delphi earlier than 2009, then use TList and store pointers to TDiagData:

    NodeData^ := PDiagData(FDiagDataList[c])^;
    

    If you're using Delphi 2009 or later, then use TList<TDiagData>, and then get rid of the type cast:

    NodeData^ := FDiagDataList[c];
    

    Either way, you'll probably find things easier to manage if every event handler starts out the same way, with a call to GetNodeData to fetch the type-safe pointer to the current node's data.

    0 讨论(0)
提交回复
热议问题