Check all checkboxes in checkboxlist with one click using c#
Asked Answered
C

6

35

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.

Clifford answered 27/12, 2012 at 8:15 Comment(1)
@Likurg, I've tried this, seems fine but didn't work for me: for(int i = 1; i < checkedlistBox.Items.Count; i++) checkedlistBox.SetItemChecked (i, true);Clifford
A
87
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}
Adulation answered 27/12, 2012 at 8:29 Comment(1)
I've tried this code a while ago but not working., And now it is., Magic.. :) Thank you @SekaiCode.Clifford
E
6

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.

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;    
}
Everlasting answered 17/12, 2015 at 10:26 Comment(0)
L
6

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);
Leeke answered 8/4, 2020 at 16:47 Comment(0)
A
2

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
    }
Acescent answered 3/6, 2014 at 21:50 Comment(0)
M
1

Try this:

 foreach(Control c in this.Controls) {
    if (c.GetType() == typeof(CheckBox)) {
       ((CheckBox)c).Checked = true;
    }
 }
Meeting answered 27/12, 2012 at 8:33 Comment(0)
T
0

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:

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;
        }
    }
}
Topdress answered 20/2, 2020 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.