如何基于字符串路径以编程方式选择或滚动到 TreeView 中的 TreeNode?

发布于 2024-09-12 16:42:30 字数 607 浏览 4 评论 0原文

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

问题是,我们有 EnsureVisible 方法,但并不完全相信它的作用与它所说的一样。没有明确的“setVisible”为 false 属性。这意味着默认情况下所有 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)
        //   {}
    }

你可以看到,我已经开始解决这个问题,但在一个简单的测试用例没有产生任何效果后,我遇到了障碍!

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 !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-09-19 16:42:30

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

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

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.

揽清风入怀 2024-09-19 16:42:30

解决方案如下:

正在工作并经过测试:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文