D
dvestal
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?
}
}
}
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?
}
}
}