Another way is to utilize the fact that when you programmatically check/uncheck, it does not give focus to the checkbox. So you can use the Focused
property as a flag.
private void SelectAllCheckBox_CheckedChanged(object sender, EventArgs e)
{
if(!((CheckBox)sender).Focused)
return;
//your code to uncheck/check all CheckedListBox here
}
No need to create another separate bool flag (unless if you are manually changing the focus state somewhere).