Clear checked items from a listbox without firing itemcheck event
I got a checkedlist box in form2 (clbForm2) where i'm explicity mapping it to an item check event. Now i need to uncheck all the che开发者_高级运维cked items of the control in Form1, from within form2. On unchecking the items, it'z firing the item check event. Is there any way to skip the event. I'd written code within that, which i dont want to run when called from form2. Please suggest a good way.
Unbind the event and rebind it.
_checkBox.CheckedChanged -= new System.EventHandler(yourEventHandler);
// Do Check as you want.
_checkBox.CheckedChanged += new System.EventHandler(yourEventHandler);
I would prefer to use a flag rather than to unbind/rebind...
Create a class level variable such as...
private bool processCheckChange = true;
Then in you event handler do...
OnCheckedChange()
{
if(processCheckChange)
{
//Handle check change
}
}
Then when you want to uncheck all items...
UncheckAllItems()
{
processCheckChange = false;
//Uncheck all items
processCheckChange = true;
}
I think this should do the job
精彩评论