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 >>

TMS FMX UI Pack
TTMSFMXTreeView: How to dynamically add nodes

This code snippet shows how to dynamically add nodes to the TMSFMXTreeView:

procedure TForm1.FormCreate(Sender: TObject);
var
  n: TTMSFMXTreeViewNode;
  v: TTMSFMXTreeViewNode;
begin
  TMSFMXTreeView1.BeginUpdate;
  TMSFMXTreeView1.Nodes.Clear;
  TMSFMXTreeView1.Columns.Clear;
  TMSFMXTreeView1.Columns.Add.Text := 'Test';
  n := TMSFMXTreeView1.AddNode;
  n.Text[0] := 'Need load childs after in onBeforeExpandNode event';
 
  v := TMSFMXTreeView1.AddNode(n);
  v.DataString := 'virtual';
 
  TMSFMXTreeView1.EndUpdate;
end;
 
procedure TForm1.TMSFMXTreeView1BeforeExpandNode(Sender: TObject;
  ANode: TTMSFMXTreeViewVirtualNode; var ACanExpand: Boolean);
var
  v: TTMSFMXTreeViewNode;
  I: Integer;
begin
  if not Assigned(ANode.Node) then
    Exit;
 
  if ANode.Node.GetChildCount > 0 then
  begin
    v := ANode.Node.Nodes[0];
    if v.DataString = 'virtual' then
    begin
      TMSFMXTreeView1.RemoveNode(v);
      for I := 0 to 9 do
        TMSFMXTreeView1.AddNode(ANode.Node).Text[0] := 'Dynamically added node ' + inttostr(I);
    end;
  end;
end;