I’m using Treeview, I have already found both of these functions and they work perfectly:
private void CheckChildren_ParentSelected(TreeNode node, Boolean isChecked)
{
foreach (TreeNode item in node.Nodes)
{
item.Checked = isChecked;
if (item.Nodes.Count > 0)
{
this.CheckChildren_ParentSelected(item, isChecked);
}
}
}
and
private void SelectParents(TreeNode node, Boolean isChecked)
{
//MessageBox.Show(node.Parent.ToString());
if (node.Parent!=null)
{
node.Parent.Checked = isChecked;
SelectParents(node.Parent, isChecked);
}
}
here is the problem: when I call each of these functions separately in the AfterCheck event they work fine but when call both of them in the AfterCheck event nothing is happening even my form is not loading
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
CheckChildren_ParentSelected(e.Node, e.Node.Checked);
SelectParents(e.Node, e.Node.Checked);
// MessageBox.Show("hi");
}
This is not working! but when I comment on of them e.g. SelectParents function it will work, but when I have both of them nothing is happening even my form isn’t loading
Do not use MessageBox to debug code, use the debugger. This is easy to see when you set a breakpoint on the event handler. Or Debug.Print() when you get overwhelmed. Changing the Checked property causes the AfterCheck event handler to run again. Simply use a private variable to prevent the recursion: