使用c#一键检查复选框列表中的所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14051494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:32:25  来源:igfitidea点击:

Check all checkboxes in checkboxlist with one click using c#

c#winformsbuttoncheckboxchecklistbox

提问by Brenelyn

I want to have a button that once clicked, it will select all checkboxes in my checklistbox. I've search the possible answers but I always see examples for asp.net and javascript. I am using Windows form in c#. Thank you for any response.

我想要一个按钮,一旦点击,它将选择我的清单框中的所有复选框。我已经搜索了可能的答案,但我总是看到 asp.net 和 javascript 的示例。我在 c# 中使用 Windows 窗体。感谢您的任何回应。

采纳答案by SekaiCode

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}

回答by patrick choi

Try this:

尝试这个:

 foreach(Control c in this.Controls) {
    if (c.GetType() == typeof(CheckBox)) {
       ((CheckBox)c).Checked = true;
    }
 }

回答by DavidRomo

Try this...

尝试这个...

    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox[] boxes = new CheckBox[7];
        boxes[0] = this.CheckBoxID;
        boxes[1] = this.CheckBoxID;
        boxes[2] = this.CheckBoxID;
        boxes[3] = this.CheckBoxID;
        boxes[4] = this.CheckBoxID;
        boxes[5] = this.CheckBoxID;
        boxes[6] = this.CheckBoxID; //you can add checkboxes as you want

        CheckBox chkBox = (CheckBox)sender;
        string chkID = chkBox.ID;
        bool allChecked = true;

        if (chkBox.Checked == false)
            allChecked = false;

        foreach (CheckBox chkBoxes in boxes)
        {
            if (chkBox.Checked == true)
            {
                if (chkBoxes.Checked == false)
                    allChecked = false;
            }
        }
        this.CheckBoxIDALL.Checked = allChecked; //Here place the main CheckBox
    }

回答by VAMSHI PAIDIMARRI

Call a method from code behind in C# and write this piece of code, then you could be able to check/uncheck them. This checks or uncheck all the check boxes present in the checkboxlist. Hope it might help.

从 C# 中的代码后面调用一个方法并编写这段代码,然后您就可以选中/取消选中它们。这将选中或取消选中复选框列表中存在的所有复选框。希望它可能会有所帮助。

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;    
}

回答by Chen10b

what I did is I put it inside of a tableLayoutPanel, I fixed all the checkboxs in the 3rd column and i added the event:

我所做的是将它放在 tableLayoutPanel 中,我修复了第 3 列中的所有复选框并添加了事件:

private void cbCheckAllCHECKBOXs_CheckedChanged(objects sender, EventArgs e)
{
    if (cbCheeckAllCHECKBOXs.Checked)
    {
        for (int i = 0; i < tlpCHECKBOXsControlPanel.RowCount; i++)
        {
            ((System.Windows.Forms.CheckBox)(tlpCHECKBOXsControlPanel.GetControlFromPosition(3, i))).Checked = true;
        }
    }
}

回答by djv

After arriving at this question multiple times, I have decided I will solve it for myself once and for all, with an extension method.

多次到达这个问题后,我决定我将一劳永逸地解决它,使用扩展方法。

public static class Extensions
{
    public static void CheckAll(this CheckedListBox checkedListBox, bool check)
    {
        for (int i = 0; i < checkedListBox.Items.Count; i++)
            checkedListBox.SetItemChecked(i, check);
    }
}
MyCheckedListBox.CheckAll(true);