• 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
                    }
                }
            }
  • 相关阅读:
    [leetcode] Best Time to Buy and Sell Stock II
    [leetcode] Best Time to Buy and Sell Stock
    [leetcode] Binary Tree Maximum Path Sum
    [leetcode] Triangle
    [leetcode] Populating Next Right Pointers in Each Node II
    [leetcode] Pascal's Triangle II
    [leetcode] Pascal's Triangle
    第三周周总结
    基础DP
    第二周周总结
  • 原文地址:https://www.cnblogs.com/perfect/p/965409.html
Copyright © 2020-2023  润新知