zoukankan      html  css  js  c++  java
  • 带checkbox的C# TreeView处理父子节点选择

    今天写了一个C#的TreeView,需要带checkbox,msdn上TreeView.AfterCheck Event (System.Windows.Forms)之处理了子节点的递归选择问题,贴一下我写的父子节点递归选择。



        1         void tree_AfterCheck(object sender, TreeViewEventArgs e)

        2         {

        3             if (e.Action != TreeViewAction.Unknown)

        4             {

        5                 UpdateCheckStatus(e);

        6             }

        7         }

        8 

        9         // update check status for parent and child

       10         private void UpdateCheckStatus(TreeViewEventArgs e)

       11         {

       12             CheckAllChildNodes(e.Node);

       13             UpdateAllParentNodes(e.Node);         

       14         }

       15 

       16         // updates all parent nodes recursively.

       17         private void UpdateAllParentNodes(TreeNode treeNode)

       18         {

       19             TreeNode parent = treeNode.Parent;

       20             if (parent != null)

       21             {

       22                 if (parent.Checked && !treeNode.Checked)

       23                 {

       24                     parent.Checked = false;

       25                     UpdateAllParentNodes(parent);

       26                 }

       27                 else if (!parent.Checked && treeNode.Checked)

       28                 {

       29                     bool all = true;

       30                     foreach (TreeNode node in parent.Nodes)

       31                     {

       32                         if (!node.Checked)

       33                         {

       34                             all = false;

       35                             break;

       36                         }

       37                     }

       38                     if (all)

       39                     {

       40                         parent.Checked = true;

       41                         UpdateAllParentNodes(parent);

       42                     }

       43                 }

       44             }

       45         }

       46 

       47         // updates all child tree nodes recursively.

       48         private void CheckAllChildNodes(TreeNode treeNode)

       49         {

       50             foreach (TreeNode node in treeNode.Nodes)

       51             {

       52                 node.Checked = treeNode.Checked;

       53                 if (node.Nodes.Count > 0)

       54                 {

       55                     // If the current node has child nodes, call the CheckAllChildsNodes method recursively.

       56                     this.CheckAllChildNodes(node);

       57                 }

       58             }

       59         }






  • 相关阅读:
    pug模板引擎(原jade)之 分支条件
    pug模板引擎(原jade)之 属性
    hosts的作用
    typeScript 之 (7) 构造函数和this
    typeScript 之 (8) 继承,重写,super
    typeScript 之(6) 类的简介
    MySQL:一条更新语句是如何执行的
    Centos7 使用 Kubeadm 搭建 k8s 集群
    HTTP Keep-Alive模式客户端与服务器如何判定传输完成
    MySQL:一条SQL是如何执行的
  • 原文地址:https://www.cnblogs.com/fresky/p/1867420.html
Copyright © 2011-2022 走看看