RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TTreeNodes.Add Method

Adds a new tree node to a tree view control.

Pascal
function Add(Sibling: TTreeNode; const S: string): TTreeNode;
C++
__fastcall TTreeNode Add(TTreeNode Sibling, const AnsiString S);

The node is added as the last sibling of the Node parameter. The S parameter specifies the Text property of the new node. Add returns the node that has been added. If the tree view is sorted, Add inserts the node in the correct sort order position rather than as the last child of the Node parameter's parent.  

C++ Examples: 

 

/*
The following example demonstrates how to add nodes and
child nodes to a TTreeView control.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TTreeNode *Node1;
  TreeView1->Items->Clear(); // remove any existing nodes
  // Add a root node
  TreeView1->Items->Add(NULL, "RootNode1");

  /* Set MyTreeNode to first node in tree view and add a child node to it */
  Node1 = TreeView1->Items->Item[0];
  TreeView1->Items->AddChild(Node1,"ChildNode1");

  // Add another root node
  TreeView1->Items->Add(Node1, "RootNode2");

  /* Reset Node1 to RootNode2 and add a child node to it */
  Node1 = TreeView1->Items->Item[2];
  TreeView1->Items->AddChild(Node1,"ChildNode2");

  /* Reset Node1 to ChildNode2 and add a child node to it */
  Node1 = TreeView1->Items->Item[3];
  TreeView1->Items->AddChild(Node1,"ChildNode2a");

   /* Add another child to ChildNode2 following ChildNode2a */
  TreeView1->Items->AddChild(Node1,"ChildNode2b");

  // add another root node
  TreeView1->Items->Add(TreeView1->Items->Item[0], "RootTreeNode3");
}

 

Delphi Examples: 

{
The following example demonstrates how to add nodes and
child nodes to a TTreeView control.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  MyTreeNode1, MyTreeNode2: TTreeNode;
begin
  with TreeView1.Items do
  begin
    Clear; { remove any existing nodes }
    MyTreeNode1 := Add(nil, 'RootTreeNode1'); { Add a root node }
    { Add a child node to the node just added }
    AddChild(MyTreeNode1,'ChildNode1');

    {Add another root node}
    MyTreeNode2 := Add(MyTreeNode1, 'RootTreeNode2');
    {Give MyTreeNode2 to a child }
    AddChild(MyTreeNode2,'ChildNode2');

    {Change MyTreeNode2 to ChildNode2 }
    { and add a child node to it}
    MyTreeNode2 := TreeView1.Items[3];
    AddChild(MyTreeNode2,'ChildNode2a');

    {Add another child to ChildNode2, after ChildNode2a }
    AddChild(MyTreeNode2,'ChildNode2b');

    {add another root node}
    Add(MyTreeNode1, 'RootTreeNode3');
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!