mosyflasher mosyflasher - 9 months ago 112
C# Question

Manage CheckedListBox ItemCheck event to run after an item checked not before

I am using

in C# Window Forms Application.

I want to do something after one item checked or unchecked but
event runs before the item checked/unchecked .
How can I do that?


CheckedListBox.ItemCheck Event

The check state is not updated until after the ItemCheck event occurs.

To run some codes after the item checked, you should use a workaround.

Best Option

You can use this option (Thanks to Hans Passant for this post):

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    this.BeginInvoke(new Action(() =>
        //Do the after check tasks here

Another option

  • If in middle of ItemCheck Event, you need to know state of item, you should use e.NewValue instead of using checkedListBox1.GetItemChecked(i)

  • If you need to pass a list of checked indices to a method do this:

Using the code:

var checkedIndices = this.checkedListBox1.CheckedIndices.Cast<int>().ToList();
if (e.NewValue == CheckState.Checked)

 //now you can do what you need to checkedIndices
 //Here if after check but you should use the local variable checkedIndices 
 //to find checked indices

Another Option

In middle of ItemCheck event, remove handler of ItemCheck, SetItemCheckState and then add handler egain.

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    var control = (CheckedListBox)sender;
    // Remove handler
    control.ItemCheck -= checkedListBox_ItemCheck;

    control.SetItemCheckState(e.Index, e.NewValue);

    // Add handler again
    control.ItemCheck += checkedListBox_ItemCheck;

    //Here is After Check, do additional stuff here