//TreeView的AfterCheck事件 private void TvNodeInfo_AfterCheck(object sender, TreeViewEventArgs e) { CheckControl(e); } public void CheckControl(TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { if (e.Node != null && !Convert.IsDBNull(e.Node)) { CheckParentNode(e.Node); if (e.Node.Nodes.Count > 0) { CheckAllChildNodes(e.Node, e.Node.Checked); } } } } private void CheckAllChildNodes(TreeNode treeNode, bool p) { foreach (TreeNode tn in treeNode.Nodes) { tn.Checked = p; if (tn.Nodes.Count > 0) { CheckAllChildNodes(tn, p); } } } private void CheckParentNode(TreeNode treeNode) { bool bChecked = false; if (treeNode.Parent != null) { foreach (TreeNode node in treeNode.Parent.Nodes) { if (node.Checked) { bChecked = true; break; } } if (bChecked) { treeNode.Parent.Checked = true; CheckParentNode(treeNode.Parent); } else { treeNode.Parent.Checked = false; CheckParentNode(treeNode.Parent); } } }