Sign in Join
FPDoc Onlineexperiment

TTreeNode.Data property

Member of TTreeNodepublicIn unit ComCtrls

Pointer to the custom data for the node.

Declaration

pascal
property Data: Pointer read FData write SetData;

Description

Data is a Pointer property with a pointer to the custom data for the node. Data can contain any arbitrary data type, and must be cast to the correct type when used in an application.

Setting a new value for the property causes the associated TreeView (when assigned) to sort the parent node or all of the TreeNodes when needed and allowed. It is needed when the tree view uses SortType stData or stBoth. It is allowed if the node is not being deleted and is bound to other tree nodes (States).

The Changed method is called with the ncDataChanged node change reason.

Example: ttreenode_data.pas

pascal
type
  TMyClass = class(TObject)
  public
    Created: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Node: TTreeNode;
begin
  if Assigned(TreeView1.Selected) then
    Node := TreeView1.Selected
  else
    Node := TreeView1.Items.GetFirstNode;

  with TreeView1.Items.AddChild(Node, Format('Node: %d', [TreeView1.Items.Count])) do
  begin
    Data := TMyClass.Create;
    TMyClass(Data).Created := FormatDateTime('hh:nn:ss',Now);
  end;
end;

procedure TForm1.TreeView1SelectionChanged(Sender: TObject);
begin
  Label1.Caption := 'Node creation time: ' + TMyClass(TreeView1.Selected.Data).Created;
end;

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.