自动检查树视图上的子节点 [英] Automatically check child nodes on a treeview

查看:136
本文介绍了自动检查树视图上的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为树视图编写代码,以便当用户检查成人节点时,也会自动检查子节点.但是当我运行它时什么都没有发生

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);
        }
    }
}



我不断收到...的错误
CS1061 C#不包含的定义,找不到可以接受第一个类型实参的扩展方法(是否缺少using指令或程序集引用?)

我尝试过的事情:



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();
}

推荐答案

如果我正确理解了您的问题,则AfterChec事件的文档中提供了示例代码.看看 TreeView.AfterCheck事件( System.Windows.Forms) [
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)[^]


^ ]会在您尝试调用方法或访问不存在的类成员时发生.
请注意,this.CheckAllChildNodes引用Form类,您可能未定义此类方法.添加以下方法以找出错误是否消失.
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.
// 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);
      }
   }
}


这篇关于自动检查树视图上的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆