在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 -= MenuControl_CheckedChanged;
rootCheck.CheckState = CheckState.Indeterminate;
rootCheck.CheckedChanged += MenuControl_CheckedChanged;
}
}
}
else
{
bool allUnChecked = true;
for (int i = 1; i < panel1.Controls.Count; i++)
{
CheckBox theCheckBox = (CheckBox)panel1.Controls[i];
allUnChecked = allUnChecked && !theCheckBox.Checked;
}
if (allUnChecked == true)
{
rootCheck.CheckState = CheckState.Unchecked;
return;
}
else
{
rootCheck.CheckedChanged -= MenuControl_CheckedChanged;
rootCheck.CheckState = CheckState.Indeterminate;
rootCheck.CheckedChanged += MenuControl_CheckedChanged;
}
}
}
protected override bool CheckData()
{
return CheckEmpty(txtRoleName, "请输入角色名称")
&& CheckEmpty(txtRoleDescription, "请输入备注")
;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!CheckValid())
{
this.IsOK = false;
this.DialogResult = DialogResult.None;
return;
}
if (CurrentRole == null)
{
CurrentRole = new sysRole();
CurrentRole.ID = CoreData.MaxRoleID + 1;
}
CurrentRole.RoleName = txtRoleName.Text;
CurrentRole.Remark = txtRoleDescription.Text;
CurrentRole.SysRoleMenuList.Clear();
foreach (CheckBox chkMenu in pnlMenu.Controls)//有checkBox所以需要循环
{
if (chkMenu != null && chkMenu.Checked)
{
var roleMenu = new sysRoleMenu();
roleMenu.RoleID = CurrentRole.ID;
roleMenu.MenuID = int.Parse(chkMenu.Tag.ToString());
CurrentRole.SysRoleMenuList.Add(roleMenu);
}
}
}
private bool CheckValid()
{
bool hasSameName = CoreData.AllRoles.Where(p => p.RoleName == txtRoleName.Text).Any();
if (hasSameName && IsAddNew)
{
Comm.InfoMessage("角色名已经存在,请重新输入!");
txtRoleName.Focus();
txtRoleName.SelectAll();
return false;
}
return true;
}
private void FEditRight_Load(object sender, EventArgs e)
{
}
private void btnCancel_Click(object sender, EventArgs e)
{
//this.IsOK = false;
//this.DialogResult = DialogResult.None;
}
}
}
namespace LatRcs.CallPlatform.FrmSetting
{
partial class FEditRight
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.uiLabel1 = new Sunny.UI.UILabel();
this.uiLabel2 = new Sunny.UI.UILabel();
this.txtRoleName = new Sunny.UI.UITextBox();
this.txtRoleDescription = new Sunny.UI.UITextBox();
this.uiLabel3 = new Sunny.UI.UILabel();
this.pnlMenu = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.chkHisttoryRoot = new System.Windows.Forms.CheckBox();
this.checkBox7 = new System.Windows.Forms.CheckBox();
this.chkWork = new System.Windows.Forms.CheckBox();
this.chkTask = new System.Windows.Forms.CheckBox();
this.checkBox13 = new System.Windows.Forms.CheckBox();
this.checkBox12 = new System.Windows.Forms.CheckBox();
this.checkBox9 = new System.Windows.Forms.CheckBox();
this.checkBox10 = new System.Windows.Forms.CheckBox();
this.checkBox11 = new System.Windows.Forms.CheckBox();
this.panel1 = new System.Windows.Forms.Panel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox8 = new System.Windows.Forms.CheckBox();
this.panel3 = new System.Windows.Forms.Panel();
this.panel4 = new System.Windows.Forms.Panel();
this.pnlBtm.SuspendLayout();
this.pnlMenu.SuspendLayout();
this.panel2.SuspendLayout();
this.panel1.SuspendLayout();
this.panel3.SuspendLayout();
this.panel4.SuspendLayout();
this.SuspendLayout();
//
// pnlBtm
//
this.pnlBtm.Location = new System.Drawing.Point(1, 317);
this.pnlBtm.Size = new System.Drawing.Size(680, 55);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(552, 12);
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(437, 12);
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// uiLabel1
//
this.uiLabel1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLabel1.Location = new System.Drawing.Point(42, 65);
this.uiLabel1.Name = "uiLabel1";
this.uiLabel1.Size = new System.Drawing.Size(82, 25);
this.uiLabel1.TabIndex = 2;
this.uiLabel1.Text = "角色名称";
this.uiLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// uiLabel2
//
this.uiLabel2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLabel2.Location = new System.Drawing.Point(42, 109);
this.uiLabel2.Name = "uiLabel2";
this.uiLabel2.Size = new System.Drawing.Size(89, 26);
this.uiLabel2.TabIndex = 3;
this.uiLabel2.Text = "备注";
this.uiLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtRoleName
//
this.txtRoleName.ButtonSymbolOffset = new System.Drawing.Point(0, 0);
this.txtRoleName.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtRoleName.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txtRoleName.Location = new System.Drawing.Point(131, 65);
this.txtRoleName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.txtRoleName.MinimumSize = new System.Drawing.Size(1, 16);
this.txtRoleName.Name = "txtRoleName";
this.txtRoleName.Padding = new System.Windows.Forms.Padding(5);
this.txtRoleName.ShowText = false;
this.txtRoleName.Size = new System.Drawing.Size(220, 34);
this.txtRoleName.TabIndex = 4;
this.txtRoleName.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
this.txtRoleName.Watermark = "";
//
// txtRoleDescription
//
this.txtRoleDescription.ButtonSymbolOffset = new System.Drawing.Point(0, 0);
this.txtRoleDescription.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtRoleDescription.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txtRoleDescription.Location = new System.Drawing.Point(131, 109);
this.txtRoleDescription.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.txtRoleDescription.MinimumSize = new System.Drawing.Size(1, 16);
this.txtRoleDescription.Name = "txtRoleDescription";
this.txtRoleDescription.Padding = new System.Windows.Forms.Padding(5);
this.txtRoleDescription.ShowText = false;
this.txtRoleDescription.Size = new System.Drawing.Size(220, 34);
this.txtRoleDescription.TabIndex = 4;
this.txtRoleDescription.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
this.txtRoleDescription.Watermark = "";
//
// uiLabel3
//
this.uiLabel3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLabel3.Location = new System.Drawing.Point(33, 151);
this.uiLabel3.Name = "uiLabel3";
this.uiLabel3.Size = new System.Drawing.Size(71, 26);
this.uiLabel3.TabIndex = 5;
this.uiLabel3.Text = "角色权限";
this.uiLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// pnlMenu
//
this.pnlMenu.Controls.Add(this.panel4);
this.pnlMenu.Controls.Add(this.panel3);
this.pnlMenu.Controls.Add(this.panel2);
this.pnlMenu.Controls.Add(this.panel1);
this.pnlMenu.Location = new System.Drawing.Point(110, 151);
this.pnlMenu.Name = "pnlMenu";
this.pnlMenu.Size = new System.Drawing.Size(463, 158);
this.pnlMenu.TabIndex = 6;
//
// panel2
//
this.panel2.Controls.Add(this.chkHisttoryRoot);
this.panel2.Controls.Add(this.checkBox7);
this.panel2.Controls.Add(this.chkWork);
this.panel2.Controls.Add(this.chkTask);
this.panel2.Location = new System.Drawing.Point(30, 44);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(420, 37);
this.panel2.TabIndex = 7;
//
// chkHisttoryRoot
//
this.chkHisttoryRoot.AutoSize = true;
this.chkHisttoryRoot.Location = new System.Drawing.Point(3, 14);
this.chkHisttoryRoot.Name = "chkHisttoryRoot";
this.chkHisttoryRoot.Size = new System.Drawing.Size(90, 20);
this.chkHisttoryRoot.TabIndex = 0;
this.chkHisttoryRoot.Tag = "2000";
this.chkHisttoryRoot.Text = "历史模块";
this.chkHisttoryRoot.UseVisualStyleBackColor = true;
//
// checkBox7
//
this.checkBox7.AutoSize = true;
this.checkBox7.Location = new System.Drawing.Point(307, 14);
this.checkBox7.Name = "checkBox7";
this.checkBox7.Size = new System.Drawing.Size(90, 20);
this.checkBox7.TabIndex = 0;
this.checkBox7.Tag = "2003";
this.checkBox7.Text = "系统日志";
this.checkBox7.UseVisualStyleBackColor = true;
//
// chkWork
//
this.chkWork.AutoSize = true;
this.chkWork.Location = new System.Drawing.Point(195, 14);
this.chkWork.Name = "chkWork";
this.chkWork.Size = new System.Drawing.Size(90, 20);
this.chkWork.TabIndex = 0;
this.chkWork.Tag = "2002";
this.chkWork.Text = "工作日志";
this.chkWork.UseVisualStyleBackColor = true;
//
// chkTask
//
this.chkTask.AutoSize = true;
this.chkTask.Location = new System.Drawing.Point(99, 14);
this.chkTask.Name = "chkTask";
this.chkTask.Size = new System.Drawing.Size(90, 20);
this.chkTask.TabIndex = 0;
this.chkTask.Tag = "2001";
this.chkTask.Text = "任务历史";
this.chkTask.UseVisualStyleBackColor = true;
//
// checkBox13
//
this.checkBox13.AutoSize = true;
this.checkBox13.Location = new System.Drawing.Point(195, 14);
this.checkBox13.Name = "checkBox13";
this.checkBox13.Size = new System.Drawing.Size(90, 20);
this.checkBox13.TabIndex = 0;
this.checkBox13.Tag = "4002";
this.checkBox13.Text = "系统用户";
this.checkBox13.UseVisualStyleBackColor = true;
//
// checkBox12
//
this.checkBox12.AutoSize = true;
this.checkBox12.Location = new System.Drawing.Point(98, 14);
this.checkBox12.Name = "checkBox12";
this.checkBox12.Size = new System.Drawing.Size(90, 20);
this.checkBox12.TabIndex = 0;
this.checkBox12.Tag = "4001";
this.checkBox12.Text = "角色权限";
this.checkBox12.UseVisualStyleBackColor = true;
//
// checkBox9
//
this.checkBox9.AutoSize = true;
this.checkBox9.Location = new System.Drawing.Point(99, 11);
this.checkBox9.Name = "checkBox9";
this.checkBox9.Size = new System.Drawing.Size(74, 20);
this.checkBox9.TabIndex = 0;
this.checkBox9.Tag = "3001";
this.checkBox9.Text = "机器人";
this.checkBox9.UseVisualStyleBackColor = true;
//
// checkBox10
//
this.checkBox10.AutoSize = true;
this.checkBox10.Location = new System.Drawing.Point(195, 11);
this.checkBox10.Name = "checkBox10";
this.checkBox10.Size = new System.Drawing.Size(90, 20);
this.checkBox10.TabIndex = 0;
this.checkBox10.Tag = "3002";
this.checkBox10.Text = "角色统计";
this.checkBox10.UseVisualStyleBackColor = true;
//
// checkBox11
//
this.checkBox11.AutoSize = true;
this.checkBox11.Location = new System.Drawing.Point(2, 14);
this.checkBox11.Name = "checkBox11";
this.checkBox11.Size = new System.Drawing.Size(90, 20);
this.checkBox11.TabIndex = 0;
this.checkBox11.Tag = "4000";
this.checkBox11.Text = "配置模块";
this.checkBox11.UseVisualStyleBackColor = true;
//
// panel1
//
this.panel1.Controls.Add(this.checkBox1);
this.panel1.Controls.Add(this.checkBox2);
this.panel1.Controls.Add(this.checkBox3);
this.panel1.Location = new System.Drawing.Point(30, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(309, 28);
this.panel1.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(3, 5);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(90, 20);
this.checkBox1.TabIndex = 0;
this.checkBox1.Tag = "1000";
this.checkBox1.Text = "用户信息";
this.checkBox1.ThreeState = true;
this.checkBox1.UseVisualStyleBackColor = true;
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(99, 5);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(90, 20);
this.checkBox2.TabIndex = 0;
this.checkBox2.Tag = "1001";
this.checkBox2.Text = "实时角色";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(195, 5);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(74, 20);
this.checkBox3.TabIndex = 0;
this.checkBox3.Tag = "1002";
this.checkBox3.Text = "机器人";
this.checkBox3.UseVisualStyleBackColor = true;
//
// checkBox8
//
this.checkBox8.AutoSize = true;
this.checkBox8.Location = new System.Drawing.Point(3, 11);
this.checkBox8.Name = "checkBox8";
this.checkBox8.Size = new System.Drawing.Size(90, 20);
this.checkBox8.TabIndex = 0;
this.checkBox8.Tag = "3000";
this.checkBox8.Text = "统计模块";
this.checkBox8.UseVisualStyleBackColor = true;
//
// panel3
//
this.panel3.Controls.Add(this.checkBox8);
this.panel3.Controls.Add(this.checkBox10);
this.panel3.Controls.Add(this.checkBox9);
this.panel3.Location = new System.Drawing.Point(30, 83);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(413, 34);
this.panel3.TabIndex = 7;
//
// panel4
//
this.panel4.Controls.Add(this.checkBox11);
this.panel4.Controls.Add(this.checkBox12);
this.panel4.Controls.Add(this.checkBox13);
this.panel4.Location = new System.Drawing.Point(30, 120);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(312, 45);
this.panel4.TabIndex = 7;
//
// FEditRight
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(682, 375);
this.Controls.Add(this.pnlMenu);
this.Controls.Add(this.uiLabel3);
this.Controls.Add(this.txtRoleDescription);
this.Controls.Add(this.txtRoleName);
this.Controls.Add(this.uiLabel2);
this.Controls.Add(this.uiLabel1);
this.Name = "FEditRight";
this.Text = "FEditRight";
this.ZoomScaleRect = new System.Drawing.Rectangle(15, -61, 800, 450);
this.Load += new System.EventHandler(this.FEditRight_Load);
this.Controls.SetChildIndex(this.pnlBtm, 0);
this.Controls.SetChildIndex(this.uiLabel1, 0);
this.Controls.SetChildIndex(this.uiLabel2, 0);
this.Controls.SetChildIndex(this.txtRoleName, 0);
this.Controls.SetChildIndex(this.txtRoleDescription, 0);
this.Controls.SetChildIndex(this.uiLabel3, 0);
this.Controls.SetChildIndex(this.pnlMenu, 0);
this.pnlBtm.ResumeLayout(false);
this.pnlMenu.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel3.ResumeLayout(false);
this.panel3.PerformLayout();
this.panel4.ResumeLayout(false);
this.panel4.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private Sunny.UI.UILabel uiLabel1;
private Sunny.UI.UILabel uiLabel2;
private Sunny.UI.UITextBox txtRoleName;
private Sunny.UI.UITextBox txtRoleDescription;
private Sunny.UI.UILabel uiLabel3;
private System.Windows.Forms.Panel pnlMenu;
private System.Windows.Forms.CheckBox checkBox10;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox8;
private System.Windows.Forms.CheckBox chkTask;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox7;
private System.Windows.Forms.CheckBox chkWork;
private System.Windows.Forms.CheckBox checkBox13;
private System.Windows.Forms.CheckBox checkBox12;
private System.Windows.Forms.CheckBox checkBox9;
private System.Windows.Forms.CheckBox checkBox11;
private System.Windows.Forms.CheckBox chkHisttoryRoot;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Panel panel3;
}
}