Question

I'm trying to grayed out all items in the checkbox list except checked one when I check any checkbox in the list. Here is my trail, and it is going to infinite loop.

what is the issue? any idea?

private void CheckEveryOther_Click(object sender, System.EventArgs e)
{
    for (int i = 0; i < checkedListBox1.Items.Count; i++)
    {
        var state = checkedListBox1.GetItemCheckState(i);

        if (state != CheckState.Checked && state != CheckState.Indeterminate)
        {
             checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
        }
    }        
}
Était-ce utile?

La solution

private void CheckEveryOther_Click(object sender, System.EventArgs e)
{
    {Control}.{ControlsEvent} -= new  EventHandler(CheckEveryOther_Click);

    for (int i = 0; i < checkedListBox1.Items.Count; i++)
    {
        var state = checkedListBox1.GetItemCheckState(i);

        if (state != CheckState.Checked && state != CheckState.Indeterminate)
        {
             checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
        }
    } 

    {Control}.{ControlsEvent} += new  EventHandler(CheckEveryOther_Click);     
}

Replace {Control} with the control you are working with, and {ControlsEvent} with the control event that is being fired.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top