C# 树视图搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11530643/
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
TreeView search
提问by Romz
This function find only firstnode in treeview, that contains SearchText.
此函数仅查找树视图中包含 SearchText 的第一个节点。
private TreeNode SearchNode(string SearchText,TreeNode StartNode)
{
TreeNode node=null;
while (StartNode!= null)
{
if (StartNode.Text.ToLower().Contains(SearchText.ToLower()))
{
node = StartNode;
break;
};
if (StartNode.Nodes.Count != 0)
{
node=SearchNode(SearchText, StartNode.Nodes[0]);//Recursive Search
if (node != null)
{
break;
};
};
StartNode = StartNode.NextNode;
};
return node;
}
private void button1_Click(object sender, EventArgs e)
{
string SearchText = this.textBox1.Text;
if (SearchText == "")
{
return;
};
TreeNode SelectedNode = SearchNode(SearchText, treeView1.Nodes[0]);
if (SelectedNode != null)
{
this.treeView1.SelectedNode = SelectedNode;
this.treeView1.SelectedNode.Expand();
this.treeView1.Select();
};
}
How should I change it, so the function will able to find not only the first node, but all of them, every time when I click button1, it'll find next node till the end, and then it starts from the beginning. So I should search not from TreeView1.Nodes[0], but from TreeView1.SelectedNode...
我应该如何更改它,以便该函数不仅可以找到第一个节点,还可以找到所有节点,每次单击 button1 时,它都会找到下一个节点直到最后,然后从头开始。所以我不应该从 TreeView1.Nodes[0] 搜索,而是从 TreeView1.SelectedNode...
采纳答案by dash
Something like the following should be fine to add to your Form's code.
将类似于以下内容的内容添加到您的表单代码中应该没问题。
private List<TreeNode> CurrentNodeMatches = new List<TreeNode>();
private int LastNodeIndex = 0;
private string LastSearchText;
private void button1_Click(object sender, EventArgs e)
{
string searchText = this.textBox1.Text;
if (String.IsNullOrEmpty(searchText))
{
return;
};
if (LastSearchText != searchText)
{
//It's a new Search
CurrentNodeMatches.Clear();
LastSearchText = searchText;
LastNodeIndex = 0;
SearchNodes(searchText, treeView1.Nodes[0]);
}
if (LastNodeIndex >= 0 && CurrentNodeMatches.Count > 0 && LastNodeIndex < CurrentNodeMatches.Count)
{
TreeNode selectedNode = CurrentNodeMatches[LastNodeIndex];
LastNodeIndex++;
this.treeView1.SelectedNode = selectedNode;
this.treeView1.SelectedNode.Expand();
this.treeView1.Select();
}
}
private void SearchNodes(string SearchText, TreeNode StartNode)
{
TreeNode node = null;
while (StartNode != null)
{
if (StartNode.Text.ToLower().Contains(SearchText.ToLower()))
{
CurrentNodeMatches.Add(StartNode);
};
if (StartNode.Nodes.Count != 0)
{
SearchNodes(SearchText, StartNode.Nodes[0]);//Recursive Search
};
StartNode = StartNode.NextNode;
};
}
There are two parts to this;
这有两个部分;
Collect all of the nodes into a
List<TreeNode>Page through the
List<TreeNode>if the search hasn't changed. If the Search has changed, clear out the list and reset the indexing.
将所有节点收集到一个
List<TreeNode>List<TreeNode>如果搜索未更改,则翻页。如果搜索已更改,请清除列表并重置索引。
I've tested this with Windows Forms running under .Net 4 - it pages through each node in a TreeView that contain the search text, 1 by 1 until it reaches the last node.
我已经使用在 .Net 4 下运行的 Windows 窗体对此进行了测试 - 它在包含搜索文本的 TreeView 中逐个浏览每个节点,直到它到达最后一个节点。
回答by Peter Ritchie
You'll need to create a collection of nodes (like List) and add each found node to that list and return that instead of a single node. Also, you'll have to remove all the break statements
您需要创建一个节点集合(如 List)并将每个找到的节点添加到该列表中并返回它而不是单个节点。此外,您必须删除所有 break 语句
回答by adminpro
I using this solution for search contains text on tree node
我使用此解决方案进行搜索包含树节点上的文本
int currentSearch = 0;
int loop = 0;
int found = 0;
private bool FilterTreeNode(TreeNodeCollection nodes, string keyword)
{
bool result = false;
for (int i = 0; i < nodes.Count; i++)
{
if(result)
break;
loop++;
if (currentSearch < loop)
{
currentSearch++;
if (nodes[i].Text.Contains(keyword))
{
found++;
_treeView.SelectedNode = nodes[i];
_treeView.SelectedNode.Expand();
_treeView.SelectedNode.EnsureVisible();
OnFindResult(string.Format("Current result: {0} on total {1} nodes. FilePath: {2}",
found, _treeView.GetNodeCount(true), nodes[i].Name));
return true;
}
}
result = FilterTreeNode(nodes[i].Nodes, keyword);
}
return result;
}

