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

3

5

I am using CheckedListBox in C# Window Forms Application.

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

Ciel answered 29/8, 2015 at 22:31 Comment(0)
A
21

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)
    checkedIndices.Add(e.Index);
else
    if(checkedIndices.Contains(e.Index))
        checkedIndices.Remove(e.Index);

 //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      
}
Aroid answered 29/8, 2015 at 23:25 Comment(0)
U
2

Try searching more for answers, cause here it is

    private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e)
{
    CheckedListBox clb = (CheckedListBox)sender;
    // Switch off event handler
    clb.ItemCheck -= clbOrg_ItemCheck;
    clb.SetItemCheckState(e.Index, e.NewValue);
    // Switch on event handler
    clb.ItemCheck += clbOrg_ItemCheck;

    // Now you can go further
    CallExternalRoutine();        
}

And the link: Which CheckedListBox event triggers after a item is checked?

Unfleshly answered 30/8, 2015 at 0:46 Comment(0)
O
-1

You can hook up an event on ItemCheck. You can do it by right clicking your checkboxlist and select properties. And at the right side you will see the property tab, click the event tab button and locate ItemCheck event and double click it. It will generate a event method for you depend on your checkboxlist name like below.

Then, you can verify selected/checked checkbox using code below.

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    var checkBoxName = checkedListBox1.Items[e.Index];
    Console.WriteLine("Current {0}, New {1} , value {2}", e.CurrentValue, e.NewValue, checkBoxName);
}
Ochrea answered 29/8, 2015 at 23:5 Comment(1)
I see your point that CheckedListBox doesn't support an event after checked but the solution above or rather the ItemCheck event will be fired a soon as you check the checkbox in the checkboxlist and selected checkbox e.Value will be committed as soon as the event finished. I think ItemCheck event is already enough for his requirement, aren't you?Ochrea

© 2022 - 2024 — McMap. All rights reserved.