How to program a single check-box the items in a checklistbox?

2

I need to program in a single check-box, so when it is "marked" select all my files present in a checklistbox and "unchecked" the reverse.

I created a check box in my 'check / uncheck all' application, which requires that when I tag it, select all of my contributors I display in a checklistbox just below and uncheck it, uncheck the flags of all of them from that same checklistbox.

I started with something like this:

if (checkBox1.Checked == true)
    foreach (String Arquivo in ck_Colaboradores.CheckedItems)
    {

    }
else if(checkBox1.Checked == false)
    foreach (String Arquivo in ck_Colaboradores.CheckedItems)
    {

    }
    
asked by anonymous 23.10.2017 / 14:22

2 answers

6

Add an event CheckedChanged to CheckBox and change the value of CheckListBox items based on the value of CheckBox main.

private void checkBoxMarcarDesmarcarTodos_CheckedChanged(object sender, EventArgs e)
{
    var isChecked = ((CheckBox)sender).Checked;

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

See working:

    
23.10.2017 / 14:40
3

You can go through your checkbox list and assign the status of the checkbox that will control the others, for example:

for (int i = 0; i < ck_Colaboradores.Items.Count; i++)
{
    ck_Colaboradores.SetItemChecked(i, checkBox1.Checked);
}
    
23.10.2017 / 14:42