Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...