Using CheckBox in TreeView
To implement the Checked in the AfterCheck event of C # TreeView, there are mainly the following situations: 1. If the parent node is checked, all the child nodes will be checked 2. If the parent node is not checked, all child nodes will not be checked 3. If all the child nodes are unchecked, the parent node will not check 4. If the child node is partially checked, the parent node does not check 5. If all child nodes are checked, the parent node is checked. The following is the recursive implementation of private void SetNodeCheckStatus (TreeNode tn, bool chk) {if (tn = = null) return; / / Set child nodes check status foreach (TreeNode tnChild in tn.Nodes) {tnChild.Checked = chk; SetNodeCheckStatus (tnChild, chk);} / / Set parent check status int nNodeCount = 0; TreeNode tnParent = tn While (tnParent.Parent! = null) {nNodeCount = 0; tnParent = tnParent.Parent; foreach (TreeNode tnTmp in tnParent.Nodes) {if (tnTmp.Checked = = chk) {nNodeCount++;}} if (nNodeCount = = tnParent.Nodes.Count) tnParent.Checked = chk; else tnParent.Checked = false;}