using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
class Functions
{
// 树状图查找。
public static TreeNode TreeSearch(TreeNode startNode, string queryStr)
{
TreeNode node = NextNode(startNode);
if (node != null)
{
while (node != startNode)
{
if (find(node.Text, queryStr))
{
break;
}
node = NextNode(node);
}
}
return node;
}
// 树当前节点的下一个节点,先子节点,然后兄弟节点,最后一个节点的 NextNode 为第一个节点。
public static TreeNode NextNode(TreeNode node)
{
if (node != null)
{
if (node.FirstNode != null)
{
node = node.FirstNode;
}
else if (node.NextNode != null)
{
node = node.NextNode;
}
else
{
while (node.Parent != null)
{
if (node.Parent.NextNode != null)
{
node = node.Parent.NextNode;
break;
}
node = node.Parent;
}
}
}
return node;
}
// 判断节点是否是要查找的节点。
private static bool find(string nodeText, string queryText)
{
string str1 = nodeText.ToLower().Trim();
string str2 = queryText.ToLower().Trim();
return str1.Contains(str2);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
treeView1.SelectedNode = Functions.TreeSearch(treeView1.SelectedNode, textBox1.Text);
treeView1.SelectedNode = Functions.NextNode(treeView1.SelectedNode);
treeView1.Focus();
}