如何在treeView的复选框中实现选中子节点同时选中它的父节点,取消一个节点同时取消它的子节点.
在treeView的复选框中实现选中子节点同时选中它的父节点,取消一个节点同时取消它的子节点. --------------------编程问答-------------------- 有人会么,说一下 --------------------编程问答-------------------- 正在找……遇到同样地问题了 --------------------编程问答-------------------- 参考如下代码:private void TreeNodeChild(TreeNode ATreeNode, bool AChecked) // 同步子节点--------------------编程问答-------------------- private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
if (ATreeNode == null) return;
ATreeNode.Checked = AChecked;
foreach (TreeNode vTreeNode in ATreeNode.Nodes)
TreeNodeChild(vTreeNode, AChecked);
}
private void TreeNodeCheck(TreeNode ATreeNode, bool AChecked)
{
if (ATreeNode == null) return;
ATreeNode.Checked = AChecked;
if (AChecked) // 如果选中本节点这就是选中全部上级节点
{
TreeNodeCheck(ATreeNode.Parent, AChecked);
}
else
{
if (ATreeNode.Parent != null && ATreeNode.Parent.Checked)
{
foreach(TreeNode vTreeNode in ATreeNode.Parent.Nodes)
if (vTreeNode.Checked) return;
TreeNodeCheck(ATreeNode.Parent, AChecked); // 判断是否所有的兄弟节点Checked都为false
}
}
}
private bool updateTreeNode = false;
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (updateTreeNode) return; // 如果在刷新中
updateTreeNode = true;
try
{
TreeNodeChild(e.Node, e.Node.Checked); // 同步子节点
TreeNodeCheck(e.Node, e.Node.Checked);
}
finally
{
updateTreeNode = false;
}
}
{
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if (node.Nodes.Count > 0)
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
补充:.NET技术 , C#