Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# How to search a text in all parent & child nodes of treeview control winform

$
0
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);
    }
}

Thanks

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

Thanks


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>