Reputation: 3740
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);
}
}
}
Upvotes: 1
Views: 323
Reputation: 7993
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.
Upvotes: 3