Question

I'm using treeview , and I use from this code form checked and unchecked all Child Node when select parent or child node

private bool updatingTreeView;
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);
        }
    }
}
private void SelectParents(TreeNode node, Boolean isChecked)
{
    //MessageBox.Show(node.Parent.ToString());
    if (node.Parent != null)
    {
        node.Parent.Checked = isChecked;
        SelectParents(node.Parent, isChecked);
    }
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (updatingTreeView) return;
    updatingTreeView = true;
    CheckChildren_ParentSelected(e.Node, e.Node.Checked);
    SelectParents(e.Node, e.Node.Checked);
    updatingTreeView = false;
}

But now

here is the problem :

when I unchecked child node i want only unchecked all childnode in treeview.

like this picture :

nchecked all childnode

But with my code all parent and child (both) is unchecked !!!

so now i want to know how can I unchecked only all child node in treeview ,

now how to change this code for fix this problem ?

Kind Regards.

Was it helpful?

Solution

"when I unchecked child node i want only unchecked all childnode in treeview."

In other words, you don't want to uncheck parent nodes?

In that case, only call SelectParents() when a node is selected:

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (updatingTreeView) return;
    updatingTreeView = true;
    CheckChildren_ParentSelected(e.Node, e.Node.Checked);
    if (e.Node.Checked)
    {
        SelectParents(e.Node, e.Node.Checked);
    }
    updatingTreeView = false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top