Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was to write a code for a treeview, so that when the user checks the adult node, the child node is automatically checked also. But nothing happens when I run it

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    // The code only executes if the user caused the checked state to change.
    if (e.Action != TreeViewAction.Unknown)
    {
        if (e.Node.Nodes.Count > 0)
        {
            /* Calls the CheckAllChildNodes method, passing in the current
            Checked value of the TreeNode whose checked state changed. */
            this.CheckAllChildNodes(e.Node, e.Node.Checked);
        }
    }
}


I keep on getting the error of...
CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)

What I have tried:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    treeView1.BeginUpdate();
    foreach (TreeNode tn in e.Node.Nodes)
        tn.Checked = e.Node.Checked;
    treeView1.EndUpdate();
}
Posted
Updated 23-Apr-18 9:44am
v2

If I understand your question correctly, a sample code is provided in the documentation of AfterChec event. Have a look at TreeView.AfterCheck Event (System.Windows.Forms)[^]
 
Share this answer
 
Compiler Error CS1061 | Microsoft Docs[^] occurs when you try to call a method or access a class member that does not exist.
Note that this.CheckAllChildNodes refer to Form class and you probably didn't define such of method. Add below methd to find out if error dissapear.
C#
// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
   foreach(TreeNode node in treeNode.Nodes)
   {
      node.Checked = nodeChecked;
      if(node.Nodes.Count > 0)
      {
         // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
         this.CheckAllChildNodes(node, nodeChecked);
      }
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900