如何通过使用面板控件单击复选框来选中所有复选框? [英] How to checked all check box by clicking on check box using panel control ?

查看:81
本文介绍了如何通过使用面板控件单击复选框来选中所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void chkMaster_CheckedChanged(object sender,EventArgs e)

{

bool chkMaster =(((System.Windows.Forms.CheckBox(sender))。已检查) ;

checkBoxCreateFolder.Checked = chkMaster;

checkBoxCredentials.Checked = chkMaster;

checkBoxIIS.Checked = chkMaster;

checkBoxNIComment.Checked = chkMaster;

checkBoxRemove.Checked = chkMaster;

checkBoxRemove.Checked = chkMaster;

checkBoxRemove.Checked = chkMaster;

checkBoxUncomment.Checked = chkMaster;

checkBoxUninstall.Checked = chkMaster;

checkBoxUpdateHelper.Checked = chkMaster;

private void chkMaster_CheckedChanged(object sender, EventArgs e)
{
bool chkMaster = (((System.Windows.Forms.CheckBox(sender)).Checked);
checkBoxCreateFolder.Checked = chkMaster;
checkBoxCredentials.Checked = chkMaster;
checkBoxIIS.Checked = chkMaster;
checkBoxNIComment.Checked = chkMaster;
checkBoxRemove.Checked = chkMaster;
checkBoxRemove.Checked = chkMaster;
checkBoxRemove.Checked = chkMaster;
checkBoxUncomment.Checked = chkMaster;
checkBoxUninstall.Checked = chkMaster;
checkBoxUpdateHelper.Checked = chkMaster;

推荐答案

你差不多了:

You are almost there:
bool chkMaster = ((CheckBox)sender).Checked;


假设我想检查或取消检查容器Control中的所有CheckBox,如Panel:
Assuming that I wanted to check or un-check all the CheckBoxes in a container Control, like a Panel:
private void SetAllCheckBoxesCheckState(Control container, bool isChecked)
{
    foreach (CheckBox cbx in container.Controls.OfType<CheckBox>())
    {
        cbx.Checked = isChecked;
    }
}

// sample use: SetAllCheckBoxesCheckState(panel1, true);

请注意,使用Controls.OfType< SomeControlType>只返回其类型匹配的目标容器中的顶级控件(它不是递归的):如果在Panel中的面板中有一个CheckBox控件,.OfType将找不到它。

Note that using Controls.OfType<SomeControlType> only returns the top-level Controls in its target container whose Type matches (it is not recursive): if you have a CheckBox control in a panel within a Panel, .OfType will not find it.


这篇关于如何通过使用面板控件单击复选框来选中所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆