【问题标题】:how to go from TreeNode.FullPath data and get the actual treenode?如何从 TreeNode.FullPath 数据中获取实际的树节点?
【发布时间】:2010-02-16 14:31:37
【问题描述】:

我想从 TreeNode.FullPath 存储一些数据,然后我想重新扩展所有内容。有没有简单的方法?

非常感谢!

【问题讨论】:

    标签: c#-3.0 treeview winforms treenode


    【解决方案1】:

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

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

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

    【讨论】:

    • 它有效。 // 递归搜索 private TreeNode FindNode(TreeNodeCollection tncoll, String strText) { TreeNode tnFound; foreach (tncoll 中的 TreeNode tnCurr) { if (tnCurr.FullPath == strText) { return tnCurr; } tnFound = FindNode(tnCurr.Nodes, strText);如果(tnFound!= null){返回tnFound; } } 返回空值; }
    【解决方案2】:

    你可以把它写成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】:

      您可以通过比较完整路径和 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-30
        • 1970-01-01
        • 2017-07-24
        • 1970-01-01
        • 1970-01-01
        • 2010-11-11
        • 1970-01-01
        相关资源
        最近更新 更多