Learn how to create and populate TeeTree components with nodes
TeeTree provides multiple flexible methods to create tree structures and add nodes. This guide covers the essential techniques for building your tree hierarchies.
Brother nodes (siblings) share the same parent as an existing node.
var Node: TTreeNodeShape;begin // Add a brother to the selected node with Tree1.Selected.First do if Assigned(Parent) then Node := AddBrother('Brother num: ' + IntToStr(Parent.Count + 1)) else Node := Tree1.AddRoot('Root num: ' + IntToStr(Tree1.Roots.Count + 1));end;
Before calling AddBrother, check if the node has a parent. Root nodes have no parent, so you should create a new root instead.
Here’s a comprehensive example from the TeeTree demo showing different addition techniques:
procedure TAddMethodsForm.FormCreate(Sender: TObject);var MyNode: TTreeNodeShape; AnotherNode: TTreeNodeShape;begin // 1. Simple way to add a root node Tree1.Add('1.Simple'); // 2. Preserve the resulting node MyNode := Tree1.Add('2.MyNode'); // 3. Add a child node to MyNode Tree1.Add('3.Child', MyNode); // 4. Specify X and Y positions with no parent Tree1.Add(200, 100, '4.At 100,100', nil); // 5. Using MyNode's Add method MyNode.Add('5.Child'); // 6. Adding a brother MyNode.AddBrother('6.Brother'); // 7. Using Shapes.AddChild Tree1.Shapes.AddChild(MyNode, '7.AddChild'); // 8. Using default index property AnotherNode := Tree1[0].Add('8.Add Child'); // 9. Using the node Parent property AnotherNode.Parent.Add('9.Another Brother'); // 10. Chain nodes in one line Tree1.Add('10.Root').Add('Child').Add('Sub-Child');end;
Example of adding nodes interactively based on user selection:
procedure ButtonAddChildClick(Sender: TObject);var Node: TTreeNodeShape;begin // Add child to selected node with Tree1.Selected.First do begin Node := AddChild('Child num: ' + IntToStr(Count + 1)); Expanded := True; // Show the new child end; // Select the new node Tree1.Selected.Clear; Node.Selected := True;end;