473,503 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fals e);

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

May 23 '07 #1
1 5064
You need to handle not the ItemCheck event but ItemChecked:

private void list_ItemChecked( object sender, ItemCheckedEventArgs e )

{

if( !e.Item.Checked )

list.Items.Remove( e.Item );

}

The reason for this exception is that ItemCheck event is the first one in
the event queue that the form must process. For instance, the list view must
re-draw the item with its new (un)checked state. You remove the item from
the list, but the form still tries to access that item in the collection,
and it's gone. Hence, the exception.

<dv*****@gmail.comwrote in message
news:11*********************@p47g2000hsd.googlegro ups.com...
>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(fals e);

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

May 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6420
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
4
4745
by: rdi | last post by:
Why does the following NOT remove the items from the ListView? I need to remove all the items and re-build the LV based on new information. Thanks. -- RDI (remove the exclamation from...
21
5170
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
2
6278
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
7
10406
by: Progalex | last post by:
Hi everybody! I have a listview and a treeview in a form . With an OpenDialog I let the user select multiple files and then these files are added to the listview with the complete pathname,...
0
3119
by: Droopy | last post by:
Hi, I got the System.ArgumentOutOfRangeException exception when adding an item in ListView. This ListView (listViewOutput) is used to display messages to user (normal messages and error...
0
3446
by: Martin Henke | last post by:
Dear all, I've got a problem with my custom combo box. When I select an entry from it, then I get the error message: "An unhandled exception of type 'System.ArgumentOutOfRangeException'...
7
2017
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
6
7618
by: sinhzun | last post by:
Hi everybody, I have a trouble with ArgumentOutOfRangeException. When I type a French word in an AutoCompleteComboBox then submit it to add new record to database, the combo box throw an exception: ...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.