Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check all checkboxes in checkboxlist with one click using c#

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.

like image 214
Brenelyn Avatar asked Dec 27 '12 08:12

Brenelyn


4 Answers

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}
like image 155
SekaiCode Avatar answered Oct 12 '22 21:10

SekaiCode


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;    
}
like image 44
VAMSHI PAIDIMARRI Avatar answered Oct 12 '22 21:10

VAMSHI PAIDIMARRI


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);
like image 5
djv Avatar answered Oct 12 '22 20:10

djv


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
    }
like image 2
DavidRomo Avatar answered Oct 12 '22 21:10

DavidRomo