c# - TreeView 搜索

标签 c# winforms search treeview treenode

此函数仅在 TreeView 中查找包含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...

最佳答案

像下面这样的东西应该可以添加到您的表单代码中。

    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;
        };

    }

这有两个部分;

  1. 将所有节点收集到 List<TreeNode>

  2. 翻阅 List<TreeNode>如果搜索没有改变。如果搜索已更改,请清除列表并重置索引。

我已经使用在 .Net 4 下运行的 Windows 窗体对此进行了测试 - 它逐页浏览包含搜索文本的 TreeView 中的每个节点,逐一翻页,直到到达最后一个节点。

关于c# - TreeView 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11530643/

相关文章:

javascript - 用于缓存来自 RESTful 服务的搜索结果的优雅方法?

c# - 在 Xamarin UITableViewCell 中向 UILabel 添加中心约束不会使标签居中

c# - 无法将 NULL 值插入列 'module_desc'

c# - 无法删除文本框 c# 中的\r 和\n

c# - 检测到 RichTextBox 中的粘贴

php - 显示多个词的搜索结果

c# - 过滤级联 CheckedListBox 并保留项目的检查状态

c# - 使用代码隐藏堆叠列系列在 WPF 图表上旋转独立轴标签

c# - 在另一个应用程序中的 MessageBox 中捕获按钮单击事件

java - 使用 JComboBox 作为搜索框