ListView ItemCheck

csharp_boy

Newcomer
Joined
Feb 5, 2006
Messages
5
I've noticed that in .NET, the IndexChanged event runs after it changes the index, where as the ItemCheck event runs before it checks/unchecks the boxes.

Like for example, say I have a listview with 1 item in it, and I do this.

private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Debug.Writeline(listView1.SelectedItems.Count);

}

When I select an item, it'll display 1 and when I unselect all items, it'll display 0.

On the other hand, if I do this, the reverse happens.

private void listView1_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
Debug.WriteLine(listView1.CheckedItems.Count);
}

When I check an item, it'll still display 0 and when I uncheck the item, it'll display 1.

Is there any reason that Microsoft has implemented it like this?
 
The event is run before the item is actually checked (i.e. you click a checkbox, it raises an event to let you know that an item is going to be checked, and then it actually checks the item). If you look at the members of the ItemCheckEventArgs, e, you will see that this allows you to examine the old value (CurrentValue), the new value (NewValue), and the index of the item that was clicked. It doesn't really do much for you and it can be slightly counter-intuitive, but if you just keep in mind that the item that is being checked does not actually change until after the event is raised, you should be fine.
 
Back
Top