如何从 TreeNode.FullPath 数据获取实际的树节点?

发布于 2024-08-21 22:17:16 字数 77 浏览 2 评论 0原文

我想存储来自 TreeNode.FullPath 的一些数据,然后我想重新扩展到目前为止的所有内容。有简单的方法吗?

多谢!

I would like to store some data from TreeNode.FullPath and then later i would like to re-expand everything up to that point. Is there a simple way of doing it?

Thanks a lot!

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

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

发布评论

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

评论(3

写给空气的情书 2024-08-28 22:17:17

您可以使用完整路径和 TreeNode.Text 的比较来定位特定的树节点。

TreeNode currentNode;

string fullpath="a0\b0\c0";   // store treenode fullpath for example

string[] tt1 = null;
tt1 = fullpath.Split('\\');
for (int i = 0; i < tt1.Count(); i++)
{
    if (i == 0)
    {
            foreach (TreeNode tn in TreeView1.Nodes)
            {
                if (tn.Text == tt1[i])
                {
                    currentNode = tn;
                }
            }
    }
    else
    {
            foreach (TreeNode tn in currentNode.Nodes)
            {
                if (tn.Text == tt1[i])
                {
                    currentNode = tn;
                }
            }
        }
}
TreeView1.SelectedNode = currentNode;

You are able to locate the specific treenode using comparison of fullpath and TreeNode.Text.

TreeNode currentNode;

string fullpath="a0\b0\c0";   // store treenode fullpath for example

string[] tt1 = null;
tt1 = fullpath.Split('\\');
for (int i = 0; i < tt1.Count(); i++)
{
    if (i == 0)
    {
            foreach (TreeNode tn in TreeView1.Nodes)
            {
                if (tn.Text == tt1[i])
                {
                    currentNode = tn;
                }
            }
    }
    else
    {
            foreach (TreeNode tn in currentNode.Nodes)
            {
                if (tn.Text == tt1[i])
                {
                    currentNode = tn;
                }
            }
        }
}
TreeView1.SelectedNode = currentNode;
云柯 2024-08-28 22:17:16

您可以将其作为 TreeNodeCollection 的扩展方法编写:

using System;
using System.Linq;
using System.Windows.Forms;

namespace Extensions.TreeViewCollection
{
    public static class TreeNodeCollectionUtils
    {
        public static TreeNode FindTreeNodeByFullPath(this TreeNodeCollection collection, string fullPath, StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
        {
            var foundNode = collection.Cast<TreeNode>().FirstOrDefault(tn => string.Equals(tn.FullPath, fullPath, comparison));
            if (null == foundNode)
            {
                foreach (var childNode in collection.Cast<TreeNode>())
                {
                    var foundChildNode = FindTreeNodeByFullPath(childNode.Nodes, fullPath, comparison);
                    if (null != foundChildNode)
                    {
                        return foundChildNode;
                    }
                }
            }

            return foundNode;
        }
    }
}

You can write it as an extension method to the TreeNodeCollection:

using System;
using System.Linq;
using System.Windows.Forms;

namespace Extensions.TreeViewCollection
{
    public static class TreeNodeCollectionUtils
    {
        public static TreeNode FindTreeNodeByFullPath(this TreeNodeCollection collection, string fullPath, StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
        {
            var foundNode = collection.Cast<TreeNode>().FirstOrDefault(tn => string.Equals(tn.FullPath, fullPath, comparison));
            if (null == foundNode)
            {
                foreach (var childNode in collection.Cast<TreeNode>())
                {
                    var foundChildNode = FindTreeNodeByFullPath(childNode.Nodes, fullPath, comparison);
                    if (null != foundChildNode)
                    {
                        return foundChildNode;
                    }
                }
            }

            return foundNode;
        }
    }
}
不知所踪 2024-08-28 22:17:16

我遇到了类似的问题(但我只是想再次找到树节点)并发现了这个:

http://c-sharpe.blogspot.com/2010/01/get-treenode-from-full-path.html

我知道它只能回答您问题的一部分,但总比没有回复好;)

I had a similar problem (but I just wanted to find the treenode again) and found this:

http://c-sharpe.blogspot.com/2010/01/get-treenode-from-full-path.html

i know it only answers part of your problem, but better than no replies at all ;)

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