CheckBox with External CheckBox

2

I have a Form with a DropDownlist with CheckBox inside, and I have an external CheckBox that selects and unchecks all.

My question is the following.

When I unselect one of the items in the DropDown the "Select All" CheckBox should be unchecked and when I select all items it should be checked. But when I do the events are interacting with each other and are giving error.

Does anyone know a way to do this?

This is the CheckBox Select All code.

private void cbSelAll_CheckedChanged(object sender, EventArgs e)
{
    if (cbSelAll.Checked == true)
    {
         //Ao selecionar a flag Selecionar todos marca todos os itens
         for (int i = 0; i < cb.CheckBoxItems.Count; i++)
         {
              cb.CheckBoxItems[i].Checked = true;
         }
    }
    else
    {
        //Ao desmarcar a flag Selecionar todos desmarca todos os itens
        for (int i = 0; i < cb.CheckBoxItems.Count; i++)
        {
            cb.CheckBoxItems[i].Checked = false;
        }
    }
}
    
asked by anonymous 04.08.2015 / 15:38

1 answer

0

Make a flag to know when the event is being triggered by another, and not be processed:

    bool processando = false;
    private void cbSelAll_CheckedChanged(object sender, EventArgs e)
    {
        if (!processando)
        {
            processando = true;

            if (cbSelAll.Checked == true)
            {
                //Ao selecionar a flag Selecionar todos marca todos os itens
                for (int i = 0; i < cb.CheckBoxItems.Count; i++)
                {
                    cb.CheckBoxItems[i].Checked = true;
                }
            }
            else
            {
                //Ao desmarcar a flag Selecionar todos desmarca todos os itens
                for (int i = 0; i < cb.CheckBoxItems.Count; i++)
                {
                    cb.CheckBoxItems[i].Checked = false;
                }
            }


            processando = false;
        }

    }

Place the if:

if (!processando)
{
  processando = true;
//Processo
  processando = false;
}

within other events as well.

    
03.05.2017 / 15:28