C# Winform的三态CheckBox,以及批量修改Panel中的控件

在C# WinForms中,如果你想批量修改一个Panel容器内的所有CheckBox控件的状态,你可以使用foreach循环来遍历PanelControls集合。下面是一个示例,展示了如何将一个Panel内所有的CheckBox控件设为选中状态(Checked = true)。

但是要开发如下的界面,有几个重点要考虑:

1. 三态CheckBox,请参考下面代码实现

2. Panel控件中事件的挂载,控件的遍历。如果有不清晰的,请评论反馈我们讨论。

using Infrastructure;
using LatCall.Core;
using LatCall.Service;
using LatRcs.Model.Domain;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LatRcs.CallPlatform.FrmSetting
{
    public partial class FEditRight : UIEditForm
    {

        public sysRole CurrentRole { get; set; }
        public bool IsAddNew { get; internal set; } = true;
        public FEditRight(sysRole role = null)
        {
            InitializeComponent();
            foreach (var item in pnlMenu.Controls)
            {
                if (item is System.Windows.Forms.Panel)
                {
                    Panel panelCtl = (Panel)item;//强制转换
                    foreach (CheckBox ck in panelCtl.Controls)
                    {
                        ck.CheckedChanged += MenuControl_CheckedChanged;
                    }
                }
            }
           
            if (role != null)
            {
                IsAddNew = false;
                CurrentRole = role;
                txtRoleName.Text = CurrentRole.RoleName;
                txtRoleDescription.Text = CurrentRole.Remark;
                foreach (var roleMenu in role.SysRoleMenuList)
                {
                    foreach (var ChkMenu in pnlMenu.Controls)
                    {
                        if (ChkMenu is System.Windows.Forms.CheckBox)
                        {
                            CheckBox theBox = (CheckBox)ChkMenu;//强制转换
                            if (theBox.Tag.ToString() == roleMenu.MenuID.ToString())
                            {
                                theBox.Checked = true;
                            }
                        }
                    }                        
                }
            }
        }

        private void MenuControl_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = sender as CheckBox;
            var menuLevel = int.Parse(c.Tag.ToString());
            Panel editPanel = null;
            if (menuLevel < 2000)
            {
                editPanel = panel1;
            }
             else if (menuLevel < 3000)
            {
                editPanel = panel2;
            }
             else if (menuLevel < 4000)
            {
                editPanel = panel3;
            }
             else if (menuLevel < 5000)
            {
                editPanel = panel4;
            }
            //.....

            CheckBox rootCheck = (CheckBox)editPanel.Controls[0];
            if (c == rootCheck)
            {
                if (rootCheck.CheckState == CheckState.Checked)
                {
                    for (int i = 1; i < editPanel.Controls.Count; i++)
                    {
                        CheckBox theCheckBox = (CheckBox)editPanel.Controls[i];
                        theCheckBox.Checked = true;
                    }
                }
                else 
                {
                    rootCheck.CheckState = CheckState.Unchecked;
                    for (int i = 1; i < editPanel.Controls.Count; i++)
                    {
                        CheckBox theCheckBox = (CheckBox)editPanel.Controls[i];
                        rootCheck.CheckedChanged -= MenuControl_CheckedChanged;
                        theCheckBox.Checked = false;
                        rootCheck.CheckedChanged += MenuControl_CheckedChanged;
                    }
                }
                return;
            }

            if (c.Checked == true)
            {
                bool allChecked = true;
                for(int i = 1; i< editPanel.Controls.Count; i++) 
                {
                    CheckBox theCheckBox = (CheckBox)editPanel.Controls[i];
                    allChecked = allChecked && theCheckBox.Checked;                    
                }                
                if (allChecked == true)
                {
                    rootCheck.CheckState = CheckState.Checked;
                    return;
                }
                else
                {
                    bool allUnChecked = true;
                    for (int i = 1; i < editPanel.Controls.Count; i++)
                    {
                        CheckBox theCheckBox = (CheckBox)editPanel.Controls[i];
                        allUnChecked = allUnChecked && !theCheckBox.Checked;
                    }

                    if (allUnChecked == true)
                    {
                        rootCheck.CheckState = CheckState.Unchecked;
                        return;
                    }
                    else
                    {
                        rootCheck.CheckedChanged
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值