在C# WinForms中,如果你想批量修改一个Panel容器内的所有CheckBox控件的状态,你可以使用foreach循环来遍历Panel的Controls集合。下面是一个示例,展示了如何将一个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

424

被折叠的 条评论
为什么被折叠?



