¿Cómo cambiar la selección de ListBox según el estado de CheckBox y viceversa?

Tengo un ListBox filled with status of some work. To make it easier it's static, so contains pre-defined values. And I have three CheckBoxes: all, open y closed. Lo que yo quiero: enter image description hereenter image description hereenter image description hereenter image description here

1er problema: Usar CheckedChanged events to update other controls I'm generating endless loops.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if(checkBox1.Checked)
    {
         checkBox2.Checked = true;
         //etc.
    }
    //etc.
}        
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    if(checkBox2.Checked && checkBox3.Checked)
    {
         checkBox1.Checked = true; //this immediately triggers checkBox1_CheckedChanged
         //etc.
    }
    //etc.  
}

At this point it dies though I haven't event started to manage the 3rd checkBox y listBox.

2do problema: This is basically the same endless loop issue, but the other way around. If the user selects only the first three items in the listBox I want none of the CheckBoxes to be checked but the open. This again triggers the CheckedChange event, which sets the ListBox selected items, which triggers SelectedIndexChanged... and so on. How shall I break this?

preguntado el 28 de mayo de 14 a las 14:05

You get an endless loop because of checkBox2.Checked = true; in the checkBox2_CheckedChanged - remove it! -

I know why I get the endless loop. I can't remove as both the open and closed are ticked, I have to tick the all checkbox as well. This is the issue itself. -

3 Respuestas

If you want to change the checked value without triggering the event simply remove the event handler and then re-apply it.

checkBox1.CheckedChanged -= checkBox1_CheckedChanged;
checkBox1.Checked = true;
checkBox1.CheckedChanged += checkBox1_CheckedChanged;

Apply this wherever you want to set Checked = true.

contestado el 28 de mayo de 14 a las 15:05

Are you saying that I can remove the event handler and then add it back within another event? Like: private void checkBox2_CheckedChanged(object sender, EventArgs e) { checkBox1.CheckedChanged -= checkBox1_CheckedChanged; checkBox1.IsChecked = true; checkBox1.CheckedChanged += checkBox1_CheckedChanged; } - pescaderia3r

Yes inside of your eventhandler in order to set IsChecked without triggering a CheckedChanged event you would need to remove the CheckedChanged eventhandler and then re-apply it after you've set IsChecked. - matt webber

Let me try it. I'll get back soon. - pescaderia3r

This is perfect. To make it simple in all the events I remove all the other eventhandlers, do the necessary, and add them back. Works like a charm. - pescaderia3r

For the "All" check box, building off Matt's answer, I would use the following as checkBox1's CheckedChanged event...

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        checkBox2.CheckedChanged -= checkBox2_CheckedChanged;
        checkBox3.CheckedChanged -= checkBox3_CheckedChanged;
        checkBox2.Checked = checkBox1.Checked;
        checkBox3.Checked = checkBox1.Checked;
        checkBox2.CheckedChanged += checkBox2_CheckedChanged;
        checkBox3.CheckedChanged += checkBox3_CheckedChanged;
        // etc.
    }

This way, if you click the All check box it will both check and uncheck the other 2 boxes accordingly.

contestado el 28 de mayo de 14 a las 14:05

El problema aquí es el CheckedChanged-event. Try changing it to Click-event, and change the code as neccessary.

contestado el 29 de mayo de 14 a las 00:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.