zoukankan      html  css  js  c++  java
  • TreeView树,全选,反选,平级选操作

        首先事件选择,选择的是MouseUp事件。为啥?因为凡是跟Check有关的,在选中父节点或者子节点,都会二次触发。然后发生的就是死循环。

    Up事件就可以避免二次触发。Down事件呢?那就触发AfterCheck事件了。事件选好了, 直接上代码。

        处理思路:选中/取消当前节点,先选中其所有父节点,再选中其子节点

         注意平级节点处理:有平级节点选中,取消时需要遍历父节点。

                                   若有一个平级节点处于选中,则父节点为选中。

                                   若所有平级都没有选中的了,则父节点要取消选中

            /// <summary>
            /// 处理树节点选中和取消选中
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public static void treeView_MouseUpClick(object sender,MouseEventArgs e) 
            {
                TreeView tree = sender as TreeView;
                if (tree == null)
                    return;
                TreeNode node = tree.GetNodeAt(e.X, e.Y);
                if (node == null)
                    return;
                SetTreeNodeCheckBoxState(node,true,node.Checked);
                SetTreeNodeCheckBoxState(node, false, node.Checked);
            }
    
            /// <summary>
            /// 处理父节点,子节点的选中
            /// </summary>
            /// <param name="node">需要选中的树</param>
            /// <param name="isSetParentState">是否选中父节点</param>
            /// <param name="state">选中/不选中</param>
            public static void SetTreeNodeCheckBoxState(TreeNode node, bool isSetParentState, bool state)
            {
                if (node == null)
                    return;
                node.Checked = state;
                if (isSetParentState && node.Parent != null && node.Checked==true)
                {
                    SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
                }
                else if (isSetParentState && node.Parent != null && node.Checked == false)
                {
                    //处理取消选中
                    bool isSelect = false;
                    foreach(TreeNode n in node.Parent.Nodes)
                    {
                        if (n.Checked==true)
                            isSelect = true;
                    }
                    //平级都未有选中的才取消选中
                    if(isSelect==false)
                        SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
                }
                else if (!isSetParentState && node.Nodes!=null && node.Nodes.Count > 0)
                {
                    foreach (TreeNode node2 in node.Nodes)
                    {
                        SetTreeNodeCheckBoxState(node2, isSetParentState, state);
                    }
                }
            }
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    mac flutter 创建过程及遇到的问题
    MAC 终端编辑完成后如何保存:
    mac下添加环境变量
    beanstalkd队列
    小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors
    小程序通过web-view实现与h5页面之间的交互
    小程序中的web-view与h5网页之间的交互
  • 原文地址:https://www.cnblogs.com/TBhome/p/5607263.html
Copyright © 2011-2022 走看看