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

Asked

Viewed 264 times

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 'mark/deselect all' application, which I need when checking it to select all my collaborators that I display in a checkbox below and when unchecking it, to deselect them 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)
    {

    }
  • 1

    could improve the question a little with more details ?

  • Of course @Marconciliosouza , I created a checkbox in my application 'mark/deselect all', I need to mark it and select all my collaborators I bring in a checklist box below and uncheck it to uncheck the flags of all of them in that same checklist.

  • friends...would like now, the files I have selected I send to print,(all at once, and without showing to the user, exit directly in the standard printer) how do I do it ? I am not qualified to ask new questions for while anything put my code here !

  • you create another question, simple like this.

  • I am not qualified to ask new questions for now anything put my code here ! @Marconciliosouza

  • As such, it is not authorised ?

  • gives a message of "You have reached the limit of questions" by clicking the button "Ask a question "

Show 2 more comments

2 answers

6


Add an event CheckedChanged at the CheckBox and change the value of the CheckListBox based on the value of CheckBox leading.

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:

inserir a descrição da imagem aqui

  • Thank you @LINQ, it worked perfectly

  • You’re welcome, @Lucão on the left side of the response.

  • ah beauty! , I didn’t know.

  • @But you can only score one =D

  • @LINQ even I voted in his reply is much more complete :)

  • 1

    @Caiqueromero In the problemo, I voted for yours as soon as I saw it. There is room for all of us here and the two answers are correct and deserve to be accepted. I only mentioned it because I saw that the AP was scoring and).

  • I’m on, all our relaxes ^^

Show 2 more comments

3

You can scroll through your checkbox list and assign the checkbox state that will control the others, example:

for (int i = 0; i < ck_Colaboradores.Items.Count; i++)
{
    ck_Colaboradores.SetItemChecked(i, checkBox1.Checked);
}
  • Thanks @Caique Romero, I tested it your way and it also worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.