ArgumentOutOfRangeException When I Remove ListView items



I have a ListView with checkboxes. I want to remove items when the
checkboxes are unchecked, but to do so yields an
ArgumentOutOfRangeException. Is there an easy way to get around this?

A simple program that illustrates the exception is below:

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Form frm = new Form();
frm.Load += new EventHandler(frm_Load);
Application.Run(frm);

}

static void frm_Load(object sender, EventArgs e)
{
ListView list = new ListView();
list.CheckBoxes = true;
list.ItemCheck += new ItemCheckEventHandler(list_ItemCheck);

((Form) sender).Controls.Add(list);

ListViewItem item = new ListViewItem("An Item");

list.Items.Add(item);
item.Checked = true;
}

static void list_ItemCheck(object sender, ItemCheckEventArgs e)
{
((ListView) sender).Items.RemoveAt(e.Index);
// Exception is thrown after leaving this handler.
// How can I safely remove this ListView item?
}
}
}

.



Relevant Pages

  • Re: ListView vs CheckListBox
    ... The application UI has a List View control and a Text Box and a Button. ... list view displays a list of files with checkboxes besides each file name. ... ListView.CheckedListViewItemCollection checkedItems = ... > is displayed with in my ListView, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Setting ListViewItem check state to indeterminate check
    ... is that no ItemCheck event is raised between 1 to 2, ... I could disable checkboxes and use stateimages and manipulate it manually ... Add the imagelist as the StateImageList for the listview ... Set the StateImageIndex property according to what state ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Tabs in ListBox via Sendmessage API problem
    ... I just tried a VB5 listview in VB6, setting both the fullrowselect and the ... checkboxes, and it worked as expected as an exe with a manifest. ... Although this call may not be necessary with the VB5 controls, ...
    (microsoft.public.vb.general.discussion)
  • Re: Listview checkbox property
    ... Private Function AddToList(LV As ListView, ... Dim strItem As String ... I want to use checkboxes in a listview. ... and at design time the checkbox square ...
    (microsoft.public.vb.controls)
  • Re: Lists in VB.NET CF...
    ... But it checkboxes in the listview do work in the CF. ... > "Daniel Moth" wrote in message ... My problem is that I basically need a selection mechanism from a list, ...
    (microsoft.public.dotnet.framework.compactframework)

Loading