Ranorex

Locating a folder in treeview

 
Post new topic   Reply to topic    Ranorex Forum Index -> RanorexNet
View previous topic :: View next topic  
Author Message
behdad



Joined: 22 Nov 2006
Posts: 16
Location: Australia

PostPosted: Fri Dec 15, 2006 1:17 am    Post subject: Locating a folder in treeview
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: click into code to enlarge

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: click into code to enlarge


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_T REEVIEW_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: click into code to enlarge

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: click into code to enlarge

 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:

Quote:
C:\Program Files\Adobe


and you want to find the folder:

Quote:


@"My Computer\C:\Program Files\Common Files\Adobe\Color\Profiles\Recommended"



then the code will open the folders upto:

Quote:
C:\Program Files\Common Files


Then it will try to open the following following folder:

Quote:
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: click into code to enlarge


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: click into code to enlarge

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.
Back to top
View user's profile Send private message
behdad



Joined: 22 Nov 2006
Posts: 16
Location: Australia

PostPosted: Sat Dec 16, 2006 1:16 am    Post subject:
I found out how to locate the folder. All I had to do was:

Code: click into code to enlarge


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.
Back to top
View user's profile Send private message
admin
Site Admin


Joined: 05 Jul 2006
Posts: 351

PostPosted: Sun Jan 07, 2007 12:10 am    Post subject:
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: click into code to enlarge
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
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Ranorex Forum Index -> RanorexNet All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum