zoukankan      html  css  js  c++  java
  • winForm中应用带CheckBox的TreeView Virus

    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;
    }
    }
    }

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    第六章 编译并运行程序
    第五章 顺序型编程进阶
    第四章 异常
    第三章 顺序型编程
    TCP和UDP的区别和优缺点
    Java UDP Socket编程
    springboot 学习资源推荐
    springboot集成redis
    微信公众号的SpringBoot+Quartz的定时任务Demo
    远程桌面发生身份验证错误,要求的函数不受支持【WIN10家庭】或【专业版】--解决办法
  • 原文地址:https://www.cnblogs.com/virusswb/p/1233218.html
Copyright © 2011-2022 走看看