C# TreeView checkbox 联动打勾 #region 将树的checkbox选中 private void setNodeTrue(Node selNode) { Node node = selNode.Parent; if (node != null) { node.CheckState = CheckState.Checked; setNodeTrue(node); } } #endregion #region 将树的checkbox选中 private void setNodeStateTrue(Node selNode) { if (selNode.CheckBoxVisible) { selNode.CheckState = CheckState.Checked; foreach (Node node in selNode.Nodes) { setNodeStateTrue(node); } } } #endregion #region 将树的checkbox取消选中 private void setNodeStateFalse(Node selNode) { if (selNode.CheckBoxVisible) { selNode.CheckState = CheckState.Unchecked; foreach (Node node in selNode.Nodes) { setNodeStateFalse(node); } } } #endregion #region 将树的checkbox取消选中 private void setNodeFalse(Node selNode) { Node nextNode = selNode.NextNode; Node previousNode = selNode.PrevNode; Node praNode = selNode.Parent; if (praNode != null) { while (nextNode != null) { if (nextNode.Checked) { return; } nextNode = nextNode.NextNode; } while (previousNode != null) { if (previousNode.Checked) { return; } previousNode = previousNode.PrevNode; } praNode.CheckState = CheckState.Unchecked; setNodeFalse(praNode); } } #endregion #region 单击树的checkbox事件 private void tvDevice_AfterCheck(object sender, AdvTreeCellEventArgs e) { Node node = tvDevice.SelectedNode; if (node.Checked) { setNodeTrue(node); setNodeStateTrue(node); } else { setNodeStateFalse(node); setNodeFalse(node); } } #endregion 原文来自 http://hi.baidu.com/jm_li_cn/item/ce4cec163f02932bd0d66dbb
时间: 2024-11-01 10:19:36