此函数仅查找树视图中的第一个节点,其中包含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();
};
}
我应该如何更改它,以便该功能不仅可以找到第一个节点,而且可以找到所有节点,每次单击时button1
,它都会找到下一个节点直到最后,然后从头开始?所以我不应该从 搜索TreeView1.Nodes[0]
,而是从TreeView1.SelectedNode
...