我想存储来自 TreeNode.FullPath 的一些数据,然后我想重新扩展所有内容。有没有简单的方法呢?
非常感谢!
我有一个类似的问题(但我只是想再次找到树节点)并找到了这个:
http://c-sharpe.blogspot.com/2010/01/get-treenode-from-full-path.html
我知道它只回答了您的部分问题,但总比没有回复要好;)
您可以将其作为扩展方法写入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;
}
}
}
您可以使用完整路径和 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;