代码之家  ›  专栏  ›  技术社区  ›  IbrarMumtaz

如何根据字符串路径以编程方式选择或滚动到树视图中的树节点?

  •  1
  • IbrarMumtaz  · 技术社区  · 15 年前

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

    问题是,我们有了随后的可修正方法,但我们并不完全相信它能做到它所说的。没有对false属性显式的“setVisible”。默认情况下,所有树节点都将可见!!!!

    有人能想出证明它有效的解决方案吗?

    这里有一个方法存根正在使用?

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

    你可以看到,我已经开始攻击这个问题,但在一个简单的测试用例没有产生效果之后,我撞上了一个滚动块!!!!

    2 回复  |  直到 13 年前
        1
  •  2
  •   msergeant    15 年前

    TreeNodes将根据其父节点的展开条件以及控件的滚动位置可见。什么 EnsureVisible 方法的作用是展开适当的父级并将控件滚动到指定的节点。

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

        2
  •  1
  •   IbrarMumtaz    15 年前

    解决方案如下:

    工作和测试:

        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);