C# 是否有在 TreeView.Nodes 集合中搜索 TreeNode.Text 字段的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12388249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a method for searching for TreeNode.Text field in TreeView.Nodes collection?
提问by Romz
Like this:
像这样:
TreeNode[] treeNodes = treeView.Nodes.Find(searchString, true);
but I want it to search in the textfield instead of the namefield.
但我希望它在text现场而不是现场进行搜索name。
采纳答案by Habib
I am not aware of any inbuilt method but you may use LINQ
我不知道任何内置方法,但您可以使用 LINQ
TreeNode[] treeNodes = treeView.Nodes
.Cast<TreeNode>()
.Where(r => r.Text == "yourText")
.ToArray();
回答by MoonKnight
If I understand you correctly (you last question was very confusing), you can write a find method yourself as follows
如果我理解正确(你最后一个问题很混乱),你可以自己写一个find方法如下
public static TreeNode[] Find(this TreeNode motherNode, string findNodeText)
{
List<TreeNode> nodeList = new List<TreeNode>();
foreach (TreeNode childNode in motherNode.Nodes)
if (childNode.Text.Equals(findNodeText, StringComparison.CurrentCulture))
nodeList.Add(childNode);
return nodeList.ToArray<TreeNode>();
}
This method can be used like
这种方法可以像
TreeView myTreeView = new TreeView();
foreach (TreeNode node in myTreeView.Nodes)
{
TreeNode[] childNodes = node.Find("Text");
// Do something...
}
I hope this helps.
我希望这有帮助。
回答by L.B
To search all tree nodes (not only the direct child nodes) you can use the extension method below
要搜索所有树节点(不仅是直接子节点),您可以使用下面的扩展方法
var nodes = treeView1.FlattenTree()
.Where(n => n.Text == "sometext")
.ToList();
--
——
public static class SOExtension
{
public static IEnumerable<TreeNode> FlattenTree(this TreeView tv)
{
return FlattenTree(tv.Nodes);
}
public static IEnumerable<TreeNode> FlattenTree(this TreeNodeCollection coll)
{
return coll.Cast<TreeNode>()
.Concat(coll.Cast<TreeNode>()
.SelectMany(x => FlattenTree(x.Nodes)));
}
}
回答by Medexware
The following code only shows the nodes which matches the search criteria.
以下代码仅显示与搜索条件匹配的节点。
Copy the following code in the search event
在搜索事件中复制以下代码
private void tbxSearch_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
trvMenu.BeginUpdate();
if (tbxSearch.Text.Length > 0)
{
for (int i = trvMenu.Nodes.Count; i > 0 ; i--)
{
NodeFiltering(trvMenu.Nodes[i - 1], tbxSearch.Text);
}
}
trvMenu.EndUpdate();
}
Then create the serch & filter function
然后创建搜索和过滤功能
private bool NodeFiltering(TreeNode Nodo,string Texto)
{
bool resultado = false;
if (Nodo.Nodes.Count == 0)
{
if (Nodo.Text.ToUpper().Contains(Texto.ToUpper()))
{
resultado = true;
}
else
{
Nodo.Remove();
}
}
else
{
for (int i = Nodo.Nodes.Count; i > 0; i--)
{
if (NodeFiltering(Nodo.Nodes[i - 1], Texto))
resultado = true;
}
if (!resultado)
Nodo.Remove();
}
return resultado;
}
This code is pretty nice for creating Treeview menus with many levels.
这段代码非常适合创建具有多个级别的 Treeview 菜单。

