Skip to main content

Example: TreeList.Items[], TreeList.Count, Node.AddChild, Node.AddChildFirst

  • 2 minutes to read

The following sample code returns a child node of the node specified.

The AParent parameter provides the tree list node whose child node is obtained.

The AIndex parameter specifies the zero-based index of the child node required.

The AddFirst parameter specifies whether the new child node should be added at the beginning or at the end of the child nodes collection.

The GetNodeInstance method allows you to obtain a child node at the specified index. If the index is greater or equal to the number of child nodes a new child node is added. In this case, the position of the new node depends on the AddFirst parameter. Set AddFirst to True, to add a new child node at the beginning of the child nodes collection; set it to False, to add a new child node to the end of the collection.

function GetNodeInstance(AParent: TcxTreeListNode; AIndex: Integer; AddFirst: boolean = False): TcxTreeListNode;
begin
  if AIndex < AParent.Count then
    Result := AParent.Items[AIndex]
  else
    if AddFirst then
      Result := AParent.AddChildFirst
    else
      Result := AParent.AddChild;
end;