Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TAdvTreeView
Using the DataObject

The DataObject can hold any object you like, but you are responsible for cleaning up any references to avoid memory leaks.
A typical use:

type
  TPerson = class
    FName: string;
    FBirthDay: TDateTime;
  public
    constructor Create(AName: string; ABirthDay: TDateTime);
    property Name: string read FName;
    property BirthDay: TDateTime read FBirthDay;
  end;

...

implementation

...

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  n: TAdvCheckedTreeViewNode;
begin
  FPersons := TObjectList<TPerson>.Create;
  FPersons.Add(TPerson.Create(''Tom Jones'', EncodeDate(2016, 1, 1)));
  FPersons.Add(TPerson.Create(''John Appleseed'', EncodeDate(2016, 1, 2)));

  AdvCheckedTreeView1.Nodes.Clear;
  AdvCheckedTreeView1.Columns[0].Text := ''Name'';
  AdvCheckedTreeView1.Columns.Add.Text := ''Birthday'';
  for I := 0 to FPersons.Count - 1 do
  begin
    n := TAdvCheckedTreeViewNode(AdvCheckedTreeView1.Nodes.Add);
    n.Text[0] := FPersons[I].Name;
    n.Text[1] := DateToStr(FPersons[I].Birthday);
    n.CheckTypes[0] := tvntCheckBox;
    n.DataObject := FPersons[I];
  end;
end;

{ TPerson }

constructor TPerson.Create(AName: string; ABirthDay: TDateTime);
begin
  FName := AName;
  FBirthday := ABirthDay;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FPersons.Free;
end;
Now when selecting, editing, expanding a node, you can always directly use the DataObject and cast it to TPerson:

procedure TForm1.AdvCheckedTreeView1AfterCheckNode(Sender: TObject;
  ANode: TAdvTreeViewVirtualNode; AColumn: Integer);
var
  p: TPerson;
begin
  if Assigned(ANode.Node) and Assigned(ANode.Node.DataObject) then
    p := TPerson(ANode.Node.DataObject);
end;