10

I'm using Virtual Tree List for delphi 2009. I've created a tree with data such as:

type
  PTreeData = ^TTreeData;
  TTreeData = record
    FCaption: String;
    FPath: String;
  end;

I want to iterate over all elements but in specific order. I need to acquire first top level node, and then iterate over all of its children and modify FPath field. When I'm done with its children I want to get another top level node, and so on.

First of all I don't know how to itterate over top level nodes.

Thanks in advance for any tips on this

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157

1 Answers1

12

Here's how to iterate top level nodes. Please note (as Smasher left in his comment) that nodes are being initialized if needed by using GetFirst and GetNextSibling what might slow down the iteration a little bit. However you can use the GetFirstNoInit and GetNextNoInit functions (if you have nodes already initialized) and you might get better performance.

procedure TForm1.Button1Click(Sender: TObject);
var
  Data: PTreeData;
  Node: PVirtualNode;
begin
  Node := VirtualStringTree1.GetFirst;
  while Assigned(Node) do
  begin
    Data := VirtualStringTree1.GetNodeData(Node);
    // here you can access your data
    Node := VirtualStringTree1.GetNextSibling(Node);
  end;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 2
    It almost works :) Instead GetNext I need to use GetNextSibling to iterate over top level nodes only. Plz correct this so I could accept your answer. And thanks! :) – Jacek Kwiecień Mar 05 '12 at 13:47
  • 7
    But Jacek, your question explicitly says you need to iterate over all the top-level nodes *and their children*. – Rob Kennedy Mar 05 '12 at 14:32