Is there a method for searching for TreeNode.Text field in TreeView.Nodes collection?

后端 未结 4 794
长发绾君心
长发绾君心 2020-12-03 03:21

Like this:

TreeNode[] treeNodes = treeView.Nodes.Find(searchString, true);

but I want it to search in the text field instead o

相关标签:
4条回答
  • 2020-12-03 03:33

    To search all tree nodes (not only the direct child nodes) you can use the extension method below

    var nodes = treeView1.FlattenTree()
                         .Where(n => n.Text == "sometext")
                         .ToList();
    

    --

    public static class SOExtension
    {
        public static IEnumerable<TreeNode> FlattenTree(this TreeView tv)
        {
            return FlattenTree(tv.Nodes);
        }
    
        public static IEnumerable<TreeNode> FlattenTree(this TreeNodeCollection coll)
        {
            return coll.Cast<TreeNode>()
                        .Concat(coll.Cast<TreeNode>()
                                    .SelectMany(x => FlattenTree(x.Nodes)));
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:35

    If I understand you correctly (you last question was very confusing), you can write a find method yourself as follows

    public static TreeNode[] Find(this TreeNode motherNode, string findNodeText)
    {
        List<TreeNode> nodeList = new List<TreeNode>();
        foreach (TreeNode childNode in motherNode.Nodes)
            if (childNode.Text.Equals(findNodeText, StringComparison.CurrentCulture))
                nodeList.Add(childNode);
        return nodeList.ToArray<TreeNode>();
    }
    

    This method can be used like

    TreeView myTreeView = new TreeView();
    foreach (TreeNode node in myTreeView.Nodes)
    {
        TreeNode[] childNodes = node.Find("Text");
        // Do something...
    }
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-03 03:38

    I am not aware of any inbuilt method but you may use LINQ

    TreeNode[] treeNodes = treeView.Nodes
                                        .Cast<TreeNode>()
                                        .Where(r => r.Text == "yourText")
                                        .ToArray();
    
    0 讨论(0)
  • 2020-12-03 03:43

    The following code only shows the nodes which matches the search criteria.

    Copy the following code in the search event

       private void tbxSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                trvMenu.BeginUpdate();
                if (tbxSearch.Text.Length > 0)
                {
                    for (int i = trvMenu.Nodes.Count;  i > 0  ; i--)
                    {
                        NodeFiltering(trvMenu.Nodes[i - 1], tbxSearch.Text);
                    }
                }
                trvMenu.EndUpdate();
            }
    

    Then create the serch & filter function

        private bool NodeFiltering(TreeNode Nodo,string Texto)
        {
            bool resultado = false;
    
            if (Nodo.Nodes.Count == 0)
            {
                if (Nodo.Text.ToUpper().Contains(Texto.ToUpper()))
                {
                    resultado = true;
                }
                else
                {
                    Nodo.Remove();
                }
            }
            else
            {
                for (int i = Nodo.Nodes.Count; i > 0; i--)
                {
                    if (NodeFiltering(Nodo.Nodes[i - 1], Texto))
                        resultado = true;
                }
    
                if (!resultado)
                    Nodo.Remove();
            }
    
            return resultado;
        }
    

    This code is pretty nice for creating Treeview menus with many levels.

    0 讨论(0)
提交回复
热议问题