C# – What event to capture when a TreeVew node is checked/unchecked

ctreeviewwinforms

I have a node cbNode5 in a TreeView that contains 5 child nodes and I am trying to somehow capture the checking and unchecking of cbNode5 so I can mark the child nodes and check/unchecked to match the parent node. I know how to work though the nodes to check/uncheck the child nodes but what I can not figure out is what, and how to capture, the event that fires when the user checks the checkbox for a nod via either mouse click or keyboard.

I have tried the AfterCheck event but it does not seem to be working (and I do know the line to set the check box to true works as it runs fine under other events):

private void tvSteps_AfterCheck(object sender, TreeViewEventArgs e)
{
    tvSteps.Nodes["cbStep1"].Checked = true;
}

Best Answer

Look at the AfterCheck event...

The AfterCheck() event is working fine for me:

    private void tvSteps_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Name == "cbNode5")
        {
            foreach (TreeNode tn in e.Node.Nodes)
            {
                tn.Checked = e.Node.Checked;
            }
        }
    }

When I check/uncheck cbNode5, its children check/uncheck to match it.

Are you sure the key you are using is correct?

Related Topic