Locating a folder in treeview

Class library usage, coding and language questions.
behdad
Posts: 16
Joined: Wed Nov 22, 2006 10:56 pm
Location: Australia

Locating a folder in treeview

Post by behdad » Fri Dec 15, 2006 12:17 am

Hi Guys,

I am trying to write some methods to locate a folder in a treeview. Basically I want to pass the following string to my function and then it should one by one open the folders until it reaches the final folder:

Code: Select all

LocateFolderInLeftHandPane(@"My Computer\C:\Program Files\Common Files\Adobe\Color\Profiles\Recommended")
Now this code is similar for what you would normally run on the windows explorer.

My methods are:

Code: Select all


public void LocateFolderInLeftHandPane(string path)
        {
            // Break down the path
            string[] folderList = path.Split(@"\".ToCharArray());

            // Get the left hand side pane
            Control control = mSelf.FindControlId(ControlNames.FileSelectorDialog.AV_FILE_SELECTOR_TREEVIEW_CONTROLID);
            
            // Get the treeview
            TreeView tw = new TreeView(control.Handle);

            // Get the element for the treeview
            Element treeElement = tw.Element.FindChild(Role.Outline);

            // Locate 
            Locate(tw, treeElement, folderList, 0);
        }

and

Code: Select all

private void Locate(TreeView treeView, Element folder, string[] folderList, int index)
        {
            // If we have reached the last folder in the fields array then exit
            if (index > folderList.Length - 1)
            {
                return;
            }
            
            // Get the count for the children
            int childCount = folder.GetChildCount;

            // Traverse the tree
            Element node = null;
            for (int i = 0; i < childCount; i++)
            {
                // Get the node
                node = folder.GetChild(i);

                // If the specified folder is found then expand it
                if(node.Name.IndexOf(folderList[index]) > -1)
                {
                    // Expand
                    TreeNodeExpand(node);

                    // Increment to get the name of the next folder
                    index++; 
                    
                    // Find the current folder
                    node = treeView.Element.FindChild(Role.Outline);
                    
                    // Break out of the loop
                    break;
                }
            }

            // Recurse
            Locate(treeView, node, folderList, index);
        }
and

Code: Select all

 protected void TreeNodeExpand(Element folder)
        {
            // Check to see if the node is collapsed then expand it
            if ((folder.State & State.Collapsed) != 0)
            {
                // Double click the folder
                ElementDoubleClick(folder);

                // Wait for it to get expanded
                while ((folder.State & State.Collapsed) != 0)
                {
                    // Wait
                    Wait(Constants.TIMEOUT_TINY);
                }
            }
        }

Now the problem is that if you also have the following folder in your file system:
C:\Program Files\Adobe
and you want to find the folder:


@"My Computer\C:\Program Files\Common Files\Adobe\Color\Profiles\Recommended"
then the code will open the folders upto:
C:\Program Files\Common Files
Then it will try to open the following following folder:
C:\Program Files\Adobe
Now ifyou try to use the following line of code, then the node's child count is set to zero:

Code: Select all


node = treeView.Element.FindChild(Role.OutlineItem, node.Name, node.Class, node.Location);

and if you try to use the following then the node gets set to null:

Code: Select all

node = treeView.Element.FindChild(Role.Outline, node.Name, node.Class, node.Location);
Does anyone know how to fix this?

Does anyone have the code to locate a folder in the treeview?

Appreciate your help.

Thank you.

Behdad.

behdad
Posts: 16
Joined: Wed Nov 22, 2006 10:56 pm
Location: Australia

Post by behdad » Sat Dec 16, 2006 12:16 am

I found out how to locate the folder. All I had to do was:

Code: Select all


public void Locate(string[] folderList)
{
        Control control = myForm.FindControlId(TREEVIEWCONTROLID);
        TreeView tw = new TreeView(control.Handle);
        Element child = tw.Element.FindChild(Role.Outline);
        child = child.GetChild(0);
        child.Select(Selection.TakeFocus | Selection.TakeSelection);
        for (int i = 0; i < folderList.Length; i++)
        {
                System.Threading.Thread.Sleep(500);
                int selectedChild = tw.SelectedItem;
                tw.SelectItem(folderList[i], selectedChild);
                tw.SendKeys("{RIGHT}");
        }
}

Behdad.

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Sat Jan 06, 2007 11:10 pm

We found and corrected a bug in a tree view function of RanorexCore.
You can navigate the tree view path in the next release V1.1.0 as follows:

Code: Select all

LocatePath(@"My Computer\System (D:)\Ranorex\Bin\Net2.0");

static void LocatePath(string path)
{
    // Break down the path 
    string[] folderList = path.Split(@"\".ToCharArray());

    Form form = Application.FindFormClassName("ExploreWClass", 1);

    // Get the left hand side pane 
    Control control = form.FindControlId(100);
    TreeView treeView = new TreeView(control.Handle);

    // Locate the path
    bool ret = Locate(treeView, folderList);
}

static bool Locate(TreeView treeView, string[] folderList)
{
    // Get the root tree view element in the control 
    // The tree view itself has the role Role.Outline
    Element treeElement = treeView.Element.FindChild(Role.Outline);

    // Get the count of the children 
    int childCount = treeElement.GetChildCount;
    // Traverse the tree 
    int level = 0;
    for (int i = 0; i < childCount && level < folderList.Length; i++)
    {
        Element node = treeElement.GetChild(i);
        if (node == null)
            break;

        if (node.Name == folderList[level])
        {
            node.Select(Selection.TakeFocus | Selection.TakeSelection);
            Mouse.MoveToElement(node);
            treeView.SendKeys("{RIGHT}");
            if (level == folderList.Length - 1)
                return true;
            else
                level++;
        }
    }
    return false;
}
Jenö Herget
Ranorex Team