Jerry Dodge
Jerry Dodge

Reputation: 27276

Iterate recursively through nodes in a tree view?

I have a tree view which is populated already with files/folders from another procedure. I would like to iterate one by one through the items in the tree view, going in exact order from top to bottom. However, unlike a normal list, I cannot use just a simple for statement for this. I have to go into each node, etc.

How do I do this? I'm hoping there's a way I can do it without running a recursive procedure. As I iterate through these items, I don't necessarily care about parent or child nodes of the currently focused one. I just need to be able to read the Data property of each node as I pass them, as well as highlight the current one in the tree view as I go through it. For each item in this tree view, I will be performing some work and want to visually display to the user which one is currently selected during this process.

Upvotes: 6

Views: 9935

Answers (3)

davornik
davornik

Reputation: 31

After searching myself for way to populate ComboBoxEx with items from TListView in order to be able to filter data I have made recursive function which iterates through all ListView data. It is not 100% answer for yours question, but it may be usefull.

procedure TForm1.btnPopulateClick(Sender: TObject);
var
  lvl: Integer;
  mNode: TTreeNode;
//--
procedure PlaceTreeItem(nTree: TTreeNode; nLvl: Integer);
var
  nIndent, nImg: Integer;
  NextNode, LastNode: TTreeNode;
begin
  nIndent:=nLvl * 2;
  if nTree.HasChildren then nImg:=0 else nImg:=1;
  ComboBoxEx1.ItemsEx.AddItem(nTree.Text, nImg, nImg, nImg, nIndent, nTree.Data);
  if nTree.HasChildren then
    begin
      Inc(lvl);
      NextNode := nTree.getFirstChild;
      LastNode := nTree.GetLastChild;
      while NextNode <> nil do begin
        PlaceTreeItem(NextNode, lvl);
        if NextNode = LastNode then Dec(lvl);
        NextNode := NextNode.getNextSibling;
      end;
    end;
end;
//--
begin
  ComboBoxEx1.Clear;
  lvl:=0;
  mNode := TreeView1.Items.GetFirstNode;
  while Assigned(mNode) do begin
    PlaceTreeItem(mNode, 0);
    mNode := mNode.getNextSibling;
  end;
end;

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613013

In fact you can use a for loop.

var
  Node: TTreeNode;
....
for Node in TreeView.Items do
  DoSomething(Node);

This is syntactic sugar for:

for i := 0 to TreeView.Items.Count-1 do
  DoSomething(TreeView.Items[i]);

In terms of readability I would recommend the for/in loop.

In older Delphi versions that don't support the node iterator you may prefer to do it with a while loop.

Node := TreeView.Items.GetFirstNode;
while Assigned(Node) do
begin
  DoSomething(Node);
  Node := Node.GetNext;
end;

I expect there are other ways to do it. These are the only ones that I know!


LU RD makes the interesting observation that the documentation states:

Accessing tree view items by index can be time-intensive, particularly when the tree view contains many items. For optimal performance, try to design your application so that it has as few dependencies on the tree view's item index as possible.

This is quite true. For random access the code has to walk the tree, starting at the root, until the ith node is located.

However, there is an optimisation for sequential access. The Delphi tree view wrapper remembers the index of the last node located by index. The next time you ask for a node with index no more than one different from the cached node, the required node can be returned quickly. This is implemented in TTreeNodes.GetNodeFromIndex.

Upvotes: 16

bummi
bummi

Reputation: 27377

var
 i:Integer;
begin
  for I := 0 to tv.Items.Count - 1 do
      begin
        Listbox1.Items.Add(tv.Items[i].Text +' SubItems: ' + IntToStr(tv.Items[i].Count))
      end;

end;

Upvotes: 3

Related Questions