Page 1 of 1

Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 2:02 pm
by riccardo
I followed the instruction to iterate through a tree and click on each element in the tree. This does work fine if all TreeItems (and Sub Items) are expanded. As soon as I need to expand a TreeItem the items below won't be handled. This is how what I have coded so far:

Code: Select all

        public void ExtractTree()
        {
            //Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'FormA.Workstations.TreeItemWorkstations' at Center.", repo.FormA.Workstations.TreeItemWorkstationsInfo, new RecordItemIndex(-1));
            if (! repo.FormA.Workstations.TreeItemWorkstations.Expanded){
            	repo.FormA.TreeItemWorkstations.DoubleClick();
            }
                  
            foreach (TreeItem item in repo.FormA.Workstations.TreeItemWorkstations.Items)
            {            	
            	IterateTree(item);
            }
            
        }
        
        /// <summary>
        /// Iterate Tree
        /// </summary>
        /// <param name="treeItem"></param>
        public static void IterateTree(TreeItem treeItem)
        {
        	
        	// Expand Tree Item if necessary
        	if (! treeItem.Expanded)
        	{
        		treeItem.DoubleClick();
        	}
        	
        	// Loop through Items
        	foreach (TreeItem item in treeItem.Items)
        	{
        		// Click Tree Item
        		if (! treeItem.Expanded) {
        			treeItem.DoubleClick();
        		}
        		
        		IList<TreeItem> myItems = item.FindChildren<TreeItem>();
        		
        		foreach (TreeItem myItem in myItems)
        		{
        			IterateTree(myItem);
        		}
        		
        		// Click Tree Item
        		item.DoubleClick();
        		
        	}
        }

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 4:04 pm
by Ciege
It could be that the sub tree items don't exist until the parent is expanded. Since you are getting you list of tree items before the expand then the sub items will not exist in your tree items variable.
You can process the tree by expanding all the sub trees first, then getting the list of available tree items.

With many trees you can click in the first parent node and type - to close all sub items, and/or + or * to expand all tree items. You may be able to use invokeremotely to invoke the expand all action to expand all the tree items, then you can get your list and parse them.

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 4:11 pm
by riccardo
That would mean I need two routines, one which opens all the items, the second one which does the iteration, right?

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 4:17 pm
by Ciege
It can all be in one method but yes, this would be a multiple step process. One step to verify the tree is fully expanded and another step to iterate through the tree.

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 4:27 pm
by riccardo
I tried to do so, but even here I do have the problem that obviusly the childerns do not exist. The first two items get opened without any problems but then it stucks...

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 4:55 pm
by Ciege
What component (3rd party?) are you using for your tree view?

We use a DevExpress tree view in our AUT. In our tree view I can click on the top most parent tree item, type - to collapse all subordinates then type + to expand all subordinates.

Also, as I mentioned, you can look into using InvokeRemotely to invoke the ExpandAll of your tree.

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 5:07 pm
by riccardo
In this case we used Infragistics. By the way you are right, if I manually open all subitems, then the code works fine and goes through all the items.

I do not know how InovkeRemotely works. Is there any description about that?

Regards
Riccardo

Re: Iterating through Tree and open subtrees dynamically

Posted: Mon Oct 03, 2011 5:16 pm
by Ciege
First thing first... try using a simple method of expanding the tree first (like using keyboard shortcuts). No need to do anything fancy if you can do it using the keyboard.
If you cannot find a proper keyboard shortcut to do it, then search this forum about InvokeRemotely.