0

I am searching a text in all treeview's parent & child node recursively but which is not working as expected.

See my below code where i am searching text in all nodes of parent & child which is not working properly. please guide me what i need to repair.

private void txtSerach_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
        if (txtSerach.Text.Trim() != "")
        {
            if (treeView1.Nodes.Count > 0)
            {
            nodefound = false;

            TreeNodeCollection nodes = treeView1.Nodes;
            foreach (TreeNode n in nodes)
            {
                if (!nodefound)
                PrintRecursive(n);
                else
                return;
            }
            }
        }
        }
    }

bool nodefound = false;

private void PrintRecursive(TreeNode treeNode)
{
    if (treeNode.Text.ToUpper().Contains(txtSerach.Text.ToUpper().ToString()))
    {
    treeView1.SelectedNode = treeNode;
    treeView1.SelectedNode.Expand();
    treeView1.Focus();
    nodefound = true;
    return;
    }

    foreach (TreeNode tn in treeNode.Nodes)
    {
    PrintRecursive(tn);
    }
}

screen shot enter image description here

Text should be search in all parent & all nested nodes and if found then that node should be selected.

Thanks

T_Zacks
  • 101
  • 2
  • 17
  • What in your opinion is "properly" and what is not? I mean, please describe the result you expect compared to the result that your code produce. – Dmitry Arestov Jun 24 '19 at 10:16
  • Possible duplicate of [Is there a method for searching for TreeNode.Text field in TreeView.Nodes collection?](https://stackoverflow.com/questions/12388249/is-there-a-method-for-searching-for-treenode-text-field-in-treeview-nodes-collec) – nilsK Jun 24 '19 at 10:36

3 Answers3

1

Try.

        private void txtSerach_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtSearch.Text.Trim() != "")
                {
                    if (treeView1.Nodes.Count > 0)
                    {
                        TreeNodeCollection nodes = treeView1.Nodes;
                        var selected = PrintRecursive(nodes, txtSearch.Text);
                        treeView1.SelectedNode = selected;
                        treeView1.SelectedNode.Expand();
                        treeView1.Focus();
                    }
                }
            }
        }


        private TreeNode PrintRecursive(TreeNodeCollection parents, string txtSearch)
        {
            foreach (TreeNode node in parents)
            {
                if (node.Nodes != null && node.Nodes.Count > 0)
                {
                    var rs = PrintRecursive(node.Nodes, txtSearch);
                    if (rs != null)
                    {
                        return rs;
                    }
                }
                if (node.Text.ToUpper().Contains(txtSearch.ToUpper().ToString()))
                {
                    return node;
                }
            }
            return null;
        }

1

This code finds a key from a tree:

    public TreeNode Find(TreeNodeCollection nodes, string key)
    {
        key = key.ToUpper();
        Stack<TreeNode> stackNodes = new Stack<TreeNode>();
        foreach (TreeNode item in nodes)
        {
            stackNodes.Push(item);
        }

        while (stackNodes.Count > 0)
        {
            TreeNode currentNode = stackNodes.Pop();
            if (currentNode.Text.ToUpper() == key)
                return currentNode;
            else
                foreach (TreeNode item in currentNode.Nodes)
                {
                    stackNodes.Push(item);
                }
        }

        return null;
    }

You can call it like this:

    private void findButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(keyTextBox.Text.Trim()))
        {
            var result = Find(treeView1.Nodes, keyTextBox.Text.Trim());
            if (result != null)
            {
                treeView1.SelectedNode = result;
                treeView1.Focus();
            }
        }
    }

enter image description here

Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
0

Try this :

      private void txtSerach_KeyUp(object sender, KeyEventArgs e)
      {
         if (e.KeyCode == Keys.Enter)
         {
            var searchFor = txtSerach.Text.Trim().ToUpper();
            if (searchFor != "")
            {
               if (treeView1.Nodes.Count > 0)
               {
                  if (SearchRecursive(treeView1.Nodes, searchFor))
                  {
                     treeView1.SelectedNode.Expand();
                     treeView1.Focus();
                  }
               }
            }
         }
      }

      private bool SearchRecursive(IEnumerable nodes, string searchFor)
      {
         foreach (TreeNode node in nodes)
         {
            if (node.Text.ToUpper().Contains(searchFor))
            {
               treeView1.SelectedNode = node;
               return true;
            }

            if (SearchRecursive(node.Nodes, searchFor))
               return true;
         }

         return false;
      }
Steve Todd
  • 1,250
  • 6
  • 13
  • thanks for your code it is working but when i search a text like domestic which exist under multiple parent node then it is only selecting one child node but the text also found in other which are not getting selected.....what to do ? – T_Zacks Jun 24 '19 at 11:07
  • first text should be compared with all parent node if match then those parent nodes will be selected. also search in all child nodes of all parent node if found then that node will be selected. – T_Zacks Jun 24 '19 at 11:09
  • I don't believe TreeView controls support multiple selected nodes. You could implement it in the find/find next style to let you work your way through all matches. – Steve Todd Jun 24 '19 at 11:11