• TreeView的联动复选框


    方法引用于:http://www.cnblogs.com/javaca88/archive/2006/10/26/540827.html
    private void tvSysFunctionMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                
    if (e.Node != tvSysFunctionMenu.Nodes[0])
                {
                    SetChildNodeChecked(e.Node);
                    SetParentNodeChecked(e.Node);
                }
                
    else
                {
                    SetChildNodeChecked(e.Node);
                }
            }


            
    //根据父节点状态设置子节点的状态
            private void SetChildNodeChecked(TreeNode parentNode)
            {
                
    foreach (TreeNode node in parentNode.Nodes)
                {
                    node.Checked 
    = parentNode.Checked;
                    
    if (node.Nodes.Count > 0)
                    {
                        SetChildNodeChecked(node);
                    }
                }
            }

            
    //根据子节点状态设置父节点的状态
            private void SetParentNodeChecked(TreeNode childNode)
            {
                TreeNode parentNode 
    = childNode.Parent;
                
    if (!parentNode.Checked && childNode.Checked) //当子结点选中,父结点没选中时,则:选中父结点
                {
                    parentNode.Checked 
    = true;
                }
                
    else if (parentNode.Checked && !childNode.Checked) //取消选中其下全部子结点时,其父结点也取消
                {
                    
    int checks = 0//统计子结点的个数(为0时表示取消了其下全部子结点)
                    foreach (TreeNode node in parentNode.Nodes)
                    {
                        
    if (node.Checked)
                        {
                            checks
    ++;
                            
    break;
                        }
                    }
                    
    if (checks == 0//子结点都取消选中时,其父结点也取消
                    {
                        parentNode.Checked 
    = false;
                        
    if (!parentNode.Index.Equals(0))
                        {
                            SetParentNodeChecked(parentNode);
                        }
                    }
                }
            }

            
    //节点选中状态更改
            protected void TreeNodeCheckStatusChanged(object sender, TreeNodeMouseClickEventArgs e)
            {
                SetChildNodeChecked(e.Node);
                
    // 判断是否是根节点
                if (!e.Node.Index.Equals(0))
                {
                    SetParentNodeChecked(e.Node);
                }
            }

            private bool updateTreeNode = false;
            
    void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
            {
                
    if (updateTreeNode) return// 如果在刷新中
                updateTreeNode = true;
                
    try
                {
                    SetChildTreeNodeChecked(e.Node, e.Node.Checked); 
    // 同步子节点
                    SetParentTreeNodeChecked(e.Node, e.Node.Checked);
                }
                
    finally
                {
                    updateTreeNode 
    = false;
                }
            }

            
    private void SetChildTreeNodeChecked(TreeNode currentTreeNode, bool isChecked) // 同步子节点
            {
                
    if (currentTreeNode == nullreturn;
                currentTreeNode.Checked 
    = isChecked;
                
    foreach (TreeNode treeNode in currentTreeNode.Nodes)
                    SetChildTreeNodeChecked(treeNode, isChecked);
            }

            
    private void SetParentTreeNodeChecked(TreeNode currentTreeNode, bool isChecked)
            {
                
    if (currentTreeNode == nullreturn;
                currentTreeNode.Checked 
    = isChecked;
                
    if (isChecked) // 如果选中本节点,则选中全部上级节点
                {
                    SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked);
                }
                
    else
                {
                    
    if (currentTreeNode.Parent != null && currentTreeNode.Parent.Checked)
                    {
                        
    foreach (TreeNode treeNode in currentTreeNode.Parent.Nodes)
                            
    if (treeNode.Checked) return;
                        SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked); 
    // 判断是否所有的兄弟节点Checked都为false
                    }
                }
            }
  • 相关阅读:
    JavaScript的valueOf和toString
    Max SPA
    SQL Server从BAK文件还原新的数据库
    SQL Server删除log文件
    为什么要监控sql语句?如何监控?
    Mac下使用zsh不执行/etc/profile文件
    MAC下快速打开指定目录(转)
    干货满满!10分钟看懂Docker和K8S(转)
    Spring Boot Metrics监控之Prometheus&Grafana(转)
    Spring Boot Actuator:健康检查、审计、统计和监控(转)
  • 原文地址:https://www.cnblogs.com/perfect/p/965409.html
Copyright © 2020-2023  润新知