how to go from TreeNode.FullPath data and get the actual treenode?

余生颓废 提交于 2021-02-07 19:36:38

问题


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!


回答1:


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




回答2:


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



回答3:


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;


来源:https://stackoverflow.com/questions/2273577/how-to-go-from-treenode-fullpath-data-and-get-the-actual-treenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!