用两个递归来设置子节点和父节点的状态,如下

private void CheckChild(TreeNode node)
        {
            if (node.Nodes.Count != 0)
            {
                foreach (TreeNode item in node.Nodes)
                {
                    item.Checked = node.Checked;
                    CheckChild(item);
                }
            }
        }
        private void CheckParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                int count = 0;
                foreach (TreeNode item in node.Parent.Nodes)
                {
                    if (item.Checked)
                    {
                        count++;
                    }
                }
                node.Parent.Checked = (count == node.Parent.Nodes.Count);
                CheckParent(node.Parent);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            CheckChild(e.Node);
            CheckParent(e.Node);
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2021-10-06
  • 2022-12-23
  • 2021-11-20
  • 2021-12-15
  • 2022-12-23
猜你喜欢
  • 2021-06-06
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案