C# winform 递归选中TreeView子节点

        /// <summary>
        
/// 递归选中所有的自节点
        
/// </summary>
        
/// <param name="nodeThis">TreeNode</param>
        private void checkAllChildNode(TreeNode nodeThis)
        {
            foreach (TreeNode childNode in nodeThis.Nodes)
            {
                childNode.Checked = nodeThis.Checked;
                if (childNode.Nodes != null)
                {
                    checkAllChildNode(childNode);
                }
            }
        }

        /// <summary>
        
/// 如果node本身的选中会造成父节点下所有的同级子节点都被选种则选中父节点(递归选择)
        
/// </summary>
        
/// <param name="nodeThis">node</param>
        private void checkParentNode(TreeNode nodeThis)
        {
            //获取父节点
            TreeNode parentNode = nodeThis.Parent;
            if (parentNode == null)
            {
                return;
            }
            bool isAllChecked = true;
            //遍历所有同级
            foreach (TreeNode node in parentNode.Nodes)
            {
                if (node.Checked != true)
                {
                    isAllChecked = false;
                    break;
                }
            }
            parentNode.Checked = isAllChecked;
            if (parentNode.Parent != null)
            {
                checkParentNode(parentNode);
            }
        }
posted @ 2012-04-23 14:05  Xingsoft  阅读(2971)  评论(0编辑  收藏  举报