winForm中应用带CheckBox的TreeView

winForm中应用带CheckBox的TreeView
引用地址:
http://faq.csdn.net/read/216930.html

如题;

1、如何实现当子节点全部选中/取消,则父节点选中/取消

2、如何实现当父节点选中/取消,则子节点全部选中/取消

哪位能慷慨相助,最好能有源码实例,先谢谢了!
---------------------------------------------------------------

private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Action != TreeViewAction.Unknown)
{
CheckAllChildNodes(e.Node,e.Node.Checked);

//选中父节点
bool bol=true;
if(e.Node.Parent !=null)
{
for(int i=0;i<e.Node.Parent.Nodes.Count;i++)
{
if(!e.Node.Parent.Nodes[i].Checked)
bol = false;
}
e.Node.Parent.Checked = bol;
}
}
}

//选中子节点
public void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
---------------------------------------------------------------



1、如何实现当子节点全部选中/取消,则父节点选中/取消



  楼主是想子节点都选中的时候,所有的父节点都选中吗?如果是的话稍微改一下即可


private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Action != TreeViewAction.Unknown)
{
CheckAllChildNodes(e.Node,e.Node.Checked);

//选中父节点
bool bol=true;
if(e.Node.Parent !=null)
{
for(int i=0;i<e.Node.Parent.Nodes.Count;i++)
{
if(!e.Node.Parent.Nodes[i].Checked)
bol = false;
}

TreeNode tn=e.Node;
while ( tn.Parent != null )
{
tn = tn.Parent;
tn.Checked = bol;
}
tn.Checked = bol;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值