views:

1433

answers:

1

What is the difference between the ListView.ItemCheck and ListView.ItemChecked events in .NET?

+7  A: 

The ItemCheck event is triggered when the checked state of an item is about to change, allowing you to examine the old and new value, and to cancel the change if you wish (by assigning the NewValue property of the eventargs parameter). ItemChecked is triggered after the check (or uncheck) is completed.

Code sample:

private void ListView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // the checked state of an item is about to change
    if (e.NewValue == CheckState.Checked)
    {
        // perform some check if this is allowed, and if not...
        e.NewValue = e.CurrentValue;
    }
}

private void ListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    // the checked state of an item has changed
}
Fredrik Mörk
An additional comment - in my scenario the listview checkbox is used to diminish the list (ie TO-DO item is done) but checking a item off would result in its replacement (in the index) showing checked off) - now with the e.NewValue = e.CurrentValue it behaves normally - items drop off and the next item doesn't replace it.
John M