Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TGUID into TTreeNode.Data?

I wish to use the Data (pointer) property of the TTreeNode by putting a TGUID into it. Anybody got an idea how to achieve this?

Here's a part of the code, might help you guys understand what I'm trying to do.

if Assigned(trNode) then
begin
  trNode := tvMain.Items.Add(trNode, dmMain.qryTreeView.FieldByName('SomeColumn').Text);
  gID := StringToGUID(dmMain.qryTreeView.FieldByName('ID').Text);
  trNode.Data := //how do I do this?
like image 224
Michiel T Avatar asked Dec 25 '22 06:12

Michiel T


1 Answers

The best way to handle this is to derive a new class from TTreeNode and give it a TGUID data member, then use the TTreeView.OnCreateNodeClass event to let the TreeView create instances of your class. This way, the RTL automatically manages the memory for your guid values for you (and lets you customize the nodes any other way you wish). To access the guids, all you have to do is type-cast the TTreeNode pointers when needed. For example:

type
  TMyTreeNode = class(TTreeNode)
  public
    Guid: TGuid;
  end;

procedure TMyForm.tvMainCreateNodeClass(Sender: TCustomTreeView; var NodeClass: TTreeNodeClass);
begin
  NodeClass := TMyTreeNode;
end;

trNode := tvMain.Items.Add(...);
TMyTreeNode(trNode).Guid := StringToGUID(...);
like image 97
Remy Lebeau Avatar answered Dec 28 '22 08:12

Remy Lebeau