C# 使用c单击复选框列表中的所有复选框

C# 使用c单击复选框列表中的所有复选框,c#,winforms,button,checkbox,checklistbox,C#,Winforms,Button,Checkbox,Checklistbox,我想有一个按钮,一旦点击,它将选择我的复选框中的所有复选框。我已经搜索了可能的答案,但我总是看到asp.net和javascript的示例。我正在使用c语言中的Windows窗体。感谢您的回复。试试以下方法: for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, true); } foreach(Control c in this.Controls) {

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

试试以下方法:

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}
 foreach(Control c in this.Controls) {
    if (c.GetType() == typeof(CheckBox)) {
       ((CheckBox)c).Checked = true;
    }
 }
试试这个

    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
    }

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

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

我所做的是将其放在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;
        }
    }
}

在多次提出这个问题之后,我决定用一种扩展的方法,一劳永逸地为自己解决这个问题

公共静态类扩展 { 公共静态无效检查所有此检查列表框检查列表框,布尔检查 { 对于int i=0;i@Likurg,我试过这个,看起来不错,但对我不起作用:forint I=1;i