Schlagwort-Archive: TTreeNodesEnumerator

How to iterate a TreeView

Normally the iteration is done by a recursive method and the use of TreeView1.Items.GetFirstNode, getNextSibling, getFirstChild… But there is an easy solution: TTreeNodesEnumerator.

var
  aNodeEnum: TTreeNodesEnumerator;
  aNode: TTreeNode;
begin
  aNodeEnum := TreeView1.Items.GetEnumerator;
  try
    while aNodeEnum.MoveNext do
    begin
      aNode := aNodeEnum.Current;
      //Do something with the Node...
      aNode.Text := aNode.Text + ' --> iterate';
    end;
  finally
    aNodeEnum.Free;
  end;

The TTreeNodesEnumerator iterates through the entire TreeView. All nodes with their children.