1

我正在实现 TreeView 控件的常见场景之一,并且正在使用驱动器、文件和文件夹。制作文件系统。这意味着每个节点都可能有一条路径。

问题是,我们有 ensureVisible 方法,但我并不完全相信它会像它所说的那样做。false 属性没有明确的“setVisible”。这意味着默认情况下所有 TreeNode 都将是可见的!

有人可以提出证明它确实有效的解决方案吗?

这是一个正在使用的方法存根?

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

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

您可以看到我已经开始解决这个问题,但是在一个简单的测试用例产生无效后我遇到了一个绊脚石!!!

4

2 回答 2

2

TreeNodes 的可见性取决于其父节点的展开情况以及控件的滚动位置。EnsureVisible 方法的作用是展开正确的父节点并将控件滚动到指定节点。

假设您的树已经填充了节点,您应该能够调用EnsureVisible最后一个节点并且控件将展开所有父节点。然后您可以设置SelectedNode选择该节点。

于 2010-08-05T18:04:05.923 回答
1

这是解决方案:

工作和测试:

    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);
于 2010-08-06T00:29:30.203 回答