vba treeview选中节点 同时勾选复选框
1个回答
2015-04-09
展开全部
果勾选所有子节点,父节点也会勾选;如果某子节点取消勾选,则所有父节点也取消勾选”,下面的代码能全做到:
//设置标志,防止死循环
bool check = false;
//节点勾选后事件,如果更改某一节点状态会自动触发,所以在后面的方法中无需递归
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if(check==false)
setchild(e.Node);
setparent(e.Node);
check = false;
}
//设置子节点状态
private void setchild(TreeNode node)
{
foreach (TreeNode child in node.Nodes)
{
child.Checked = node.Checked;
}
check = true;
}
//设置父节点状态
private void setparent(TreeNode node)
{
if (node.Parent != null)
{
//如果当前节点状态为勾选,则需要所有兄弟节点都勾选才能勾选父节点
if (node.Checked)
foreach (TreeNode brother in node.Parent.Nodes)
{
if (brother.Checked == false)
return;
}
node.Parent.Checked = node.Checked;
}
}
//设置标志,防止死循环
bool check = false;
//节点勾选后事件,如果更改某一节点状态会自动触发,所以在后面的方法中无需递归
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if(check==false)
setchild(e.Node);
setparent(e.Node);
check = false;
}
//设置子节点状态
private void setchild(TreeNode node)
{
foreach (TreeNode child in node.Nodes)
{
child.Checked = node.Checked;
}
check = true;
}
//设置父节点状态
private void setparent(TreeNode node)
{
if (node.Parent != null)
{
//如果当前节点状态为勾选,则需要所有兄弟节点都勾选才能勾选父节点
if (node.Checked)
foreach (TreeNode brother in node.Parent.Nodes)
{
if (brother.Checked == false)
return;
}
node.Parent.Checked = node.Checked;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询