问题
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