Hello, I have a listview in detailed view with 1 column, and checkboxes. So basically the listview looks like a checkbox listbox. What I need to do is when an item is unchecked in the listview the item should be removed. The way it works is that there can be no items in the list view that are unchecked. To do this I handle the ItemCheck event:

Code:
private void lvMain_ItemCheck(object sender, ItemCheckEventArgs e)
{
    //...among other things
    lvUsers.Items.RemoveAt(e.Index);
}
However this crashes the application in main() in Application.Run() line with ArgumentOutOfRangeException. This only happens when the last item is removed. If you need a basic case just create a new Windows application, put a listview as I've described above, call it lvMain, and in the Form's c'tor do:

Code:
 for (int i = 0; i < 5; i++)
{
    String text = "Item ";
    text = text + i.ToString();
    ListViewItem item = new ListViewItem(text, i);
    item.Checked = true;
    lvMain.Items.Add(item);
}
and handle the ItemCheck event:
Code:
private void lvMain_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Unchecked)
        lvMain.Items.RemoveAt(e.Index);
}
Note that the exception is not thrown in the event handler and hence cannot be caught and handled there. I can't see anywhere in the docs that it is dangerous to remove item in ItemCheck event. Also it works perfectly fine for other items, just not the last item in the listview.

What am I doing wrong?

Thanks,
Latem