如何在c#.net中搜索类似于Outlook的树形文本
本文关键字:Outlook 文本 类似于 搜索 net | 更新日期: 2023-09-27 18:06:13
我正在学习c#。Net项目与inffragistics工具。在我的项目中,我想实现搜索选项,像Microsoft Office Outlook 2007的搜索功能。
说明使用System.Windows.Forms.TreeView的例子。假设同样的情况也适用于inffragistics树视图。关键是在处理树时使用递归方法:
// Returns the node with the first hit, or null if none
public TreeNode Search(TreeView treeView, string text)
{
return SearchNodes(treeView.Nodes, text);
}
// Recursive text search depth-first.
private TreeNode SearchNodes(TreeNodeCollection nodes, string text)
{
foreach (TreeNode node in nodes)
{
if (node.Text.Contains(text)) return node;
var subSearchHit = SearchNodes(node.Nodes, text);
if (subSearchHit != null) return subSearchHit;
}
return null;
}