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);
}
}
Text should be search in all parent & all nested nodes and if found then that node should be selected.
Thanks