Forum Discussion

hulig4n's avatar
hulig4n
New Contributor
14 years ago

how to loop through all nodes of a TreeView

Hi!



Im trying to loop through all nodes of a TreeView. I know how to do that in Delphi (with recursion), but I can't manage this in TestComplete. Here is what I have done so far:

1. Made a simple delphi application containing only one form with a TreeView on it (containg about 200 nodes).

2. Made a keyword test that opens the application and clicks on one node, then closes the app.

3. Wrote a script (Delphi) that should go throught all nodes and click every one, but i only manage to go through one level of the tree (the first one).



Here is the code of the script:



procedure Main;

var

  r: OleVariant;

  i: integer;  

begin

  r:= NameMapping.Sys.Project1.Form1.tvPrintReport;  //treeview

 

  if r.Exists then

  begin

    for i:= 0 to r.wRootItemCount-1 do //this is only the first level

      if r.wItems.Item(i).Items.Count > 0 then

        SubTree(r.wItems.Item(i));  //trying to use recursive procedure SubTree, but this wont compile

      //ShowMessage(inttostr(r.wItems.Item(i).Items.Count));   

  end;

end;



procedure SubTree(ANode: OleVariant);

var

  i: integer;

begin

  if ANode.Items.Count > 0 then

  begin

    for i:= 0 to ANode.Items.Count-1 do

      SubTree(ANode.Items.Item(i));

  end

  else

    ANode.Click;

end;



Can anyone tell me how to properly loop through all nodes?

Thanks in advance.

1 Reply

  • hulig4n's avatar
    hulig4n
    New Contributor
    Found a solution:



    procedure LoopItems(TV: OleVariant); forward;

    procedure LoopChildItems(ItemColl, Depth: OleVariant); forward;



    procedure Main;

    var p: OleVariant;

    begin

      p:= NameMapping.Sys.Bir.dlgReportBIRPIS.paDialog.paClient.paClientLeft.tvPrintReport;

      LoopItems(p);

    end;



    procedure LoopItems(TV: OleVariant);

    begin

      LoopChildItems(TV.wItems, 0);

    end;



    procedure LoopChildItems(ItemColl, Depth: OleVariant);

    var i, j, s, indent: OleVariant;

    begin

      for i:=0 to ItemColl.Count-1 do

      begin

        // Forms an indent to denote subitems

        indent := '';

        for j:=0 to Depth-1 do

          indent := indent + ' ';

       

        // Obtains item caption

        s := indent + ItemColl.Item.Text;

        Log.Message(s);

        

        if ItemColl.Item.Items <> nil then

          LoopChildItems(ItemColl.Item.Items, Depth+1)

        else

          ItemColl.Item.Click();

      end;

    end;



    Thx anyway.