Question

I am implementing one of the common scenarios of a TreeView control and I am using a drives, files and folders. to make a File System. That means each node potentially has a path.

Problem is, we have the ensureVisible method but am not entirely convinced this does what it says it does. There is no explicit 'setVisible' to false property. This meas by default all TreeNodes are going to be Visible !!!

Can someone come up with solution that proves it does work?

Heres a method stub am working with?

    public void selectTreeNodeFromPath(string path)
    { 
        //char[] delimiters = new char[]{ '\\' };
        //string[] pathArray = path.Split(delimiters);
        //int numberOfLvlsToProbe = pathArray.Length;

        // foreach (TreeNode node in treeDrives.Nodes)
        //   {}
    }

You can see that I have I started to attack this problem but I have hit a tumbling block after a simple test case yielded NO-EFFECT !!!

Was it helpful?

Solution 2

Heres the solution:

WORKING and TESTED:

    public void selectTreeNodeFromPath(string path)
    {
        // set up some delimters to split our path on.
        char[] delimiters = new char[] { '\\' };
        // split the array and store the values inside a string array.
        string[] pathArray = path.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
        // clean up this array.
        ensurePathArrayAccuracy(ref pathArray);
        // a simple celing variable.
        int numberOfLvlsToProbe = pathArray.Length;
        // a variable for to keep an eye on the level of the TreeNode.
        int currentLevel = 0;
        // this collection always get re-populated each iteration.
        TreeNodeCollection globalTreeNCollection = treeDrives.Nodes;

        do
        {
            // start iterating through the global tree node collection.
            foreach (TreeNode rootNode in globalTreeNCollection)
            {
                // only set the level if the node level is less!
                if (rootNode.Level < pathArray.Length)
                {
                    currentLevel = rootNode.Level;

                    // the 'currentLevel' variable can also be used to help index the 'pathArray' to make comparisons straightforward with current node.
                    if (rootNode.Text == pathArray[currentLevel])
                    {
                        // update our control variables and start again, the next level down.
                        globalTreeNCollection = rootNode.Nodes;
                        // once we have found the node then ...
                        break;
                    }                       
                }
                else // this portion of code means we are at the end of the 'pathArray.'
                { 
                    treeDrives.SelectedNode = rootNode;
                    //treeDrives.SelectedNode.EnsureVisible();

                    // to make sure the loop ends ... we need to update the currentLevel counter
                    // to allow the loop to end.
                    currentLevel = numberOfLvlsToProbe;
                    break;             
                }
            }
        }
        // if the 'currentLevel' is less than the 'numberOfLvlsToProbe' then we need
        // to keep on looping till we get to the end.
        while (currentLevel < numberOfLvlsToProbe);

OTHER TIPS

TreeNodes will be visible depending on the expanded condition of their parent nodes and where the scroll position of the control is. What the EnsureVisible method does is expand the proper parents and scroll the control to the specified node.

Assuming that your tree has already been populated with the nodes, you should be able to call EnsureVisible on the last node and the control will expand all the parents. Then you can set SelectedNode to have that node be selected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top