473,480 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ListViewItem.Remove() throws exception inside AfterLabelEdit

An interesting problem:
I have a ListView with LabelEdit set to TRUE. When I change the label, I
want to make some decisions as to whether the ListViewItem (that's just been
edited) should stay in the ListView or not.
The natural place to inspect this is in the AfterLabelEdit event...
If the decision is to take the item out, I used the ListViewItem.Remove().
However - check this out:
A) say that the item's index is N (indices are zero-based)
B) if there exists an item after it (i.e. with index N+1)
then this item gets quietly removed
C) if the item is the last in the list,an exception is thrown
saying that index N (requested to be removed) is non-existant.

It looks like:
D) when the item is being edited, it is taken OUT of the
ListView.Items collection. So - the original item N+1 moves up and
is temporarily assigned index N (that is how it gets removed - see
B) above)
E) if however, item N is the last in the collection and gets
temporarily taken out while being edited, then of course we get
and exception thrown as there isn't an item with index N

This can easily be proved:
1. create a new Windows Forms project
2. add a ListView to the form
3. Set LabelEdit to TRUE
4. Add 3 items to the list, with labels e.g. 'Item1', 'Item2' and 'Item3'
5. Create a handler for the AfterLabelEdit event
6. Inside this handler add this code:

ListViewItem item = listView1.Items[e.Item];
item.Remove();

7. Run the application
8. Edit the first item and press Enter -the second item is removed
9. Next, edit the last item then press Enter -exception is thrown

This is all very nice...

But - can someone suggest an elegant workaround , i.e. - where and
how can I remove items after they have been edited?

Many thanks in advance.
Jul 28 '06 #1
2 3920
Actually, what really happens is this:

1. You begin editing the first item (index 0), add characters and press
Enter
2. The AfterLabelEdit event fires for index 0. At this stage the change is
*not yet commited* to the actual item
3. You remove the item with index 0
4. Control returns to Windows that now tries to commit the change to index
0. Only problem is that you removed that item so it applies it to whatever
is at index 0 now, which happens to be your second item (i.e. the one that
previously had index 1). To you this looks like the second item is removed,
while in fact the first item is removed and the second item simply gets its
text changed

Imagine what happens when you edit the last item......
(btw, you can see that the correct item is being removed if you press Esc
instead of Enter)

To work around this use BeginInvoke to asynchronously invoke a method that
removes it, ensuring that it occurs after the event has completed.

private delegate void RemoveItemDelegate(ListViewItem item);
private void RemoveItem(ListViewItem item)
{
item.Remove();
}

private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
ListViewItem item = listView1.Items[e.Item];
listView1.BeginInvoke(new RemoveItemDelegate(RemoveItem), item);
}

/claes

"Kela" <Ke**@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
An interesting problem:
I have a ListView with LabelEdit set to TRUE. When I change the label, I
want to make some decisions as to whether the ListViewItem (that's just
been
edited) should stay in the ListView or not.
The natural place to inspect this is in the AfterLabelEdit event...
If the decision is to take the item out, I used the ListViewItem.Remove().
However - check this out:
A) say that the item's index is N (indices are zero-based)
B) if there exists an item after it (i.e. with index N+1)
then this item gets quietly removed
C) if the item is the last in the list,an exception is thrown
saying that index N (requested to be removed) is non-existant.

It looks like:
D) when the item is being edited, it is taken OUT of the
ListView.Items collection. So - the original item N+1 moves up and
is temporarily assigned index N (that is how it gets removed - see
B) above)
E) if however, item N is the last in the collection and gets
temporarily taken out while being edited, then of course we get
and exception thrown as there isn't an item with index N

This can easily be proved:
1. create a new Windows Forms project
2. add a ListView to the form
3. Set LabelEdit to TRUE
4. Add 3 items to the list, with labels e.g. 'Item1', 'Item2' and 'Item3'
5. Create a handler for the AfterLabelEdit event
6. Inside this handler add this code:

ListViewItem item = listView1.Items[e.Item];
item.Remove();

7. Run the application
8. Edit the first item and press Enter -the second item is removed
9. Next, edit the last item then press Enter -exception is thrown

This is all very nice...

But - can someone suggest an elegant workaround , i.e. - where and
how can I remove items after they have been edited?

Many thanks in advance.


Jul 28 '06 #2
Claes,
Many thanks for this. It is indeed an elegant solution.
Best Regards
Kela

"Claes Bergefall" wrote:
Actually, what really happens is this:

1. You begin editing the first item (index 0), add characters and press
Enter
2. The AfterLabelEdit event fires for index 0. At this stage the change is
*not yet commited* to the actual item
3. You remove the item with index 0
4. Control returns to Windows that now tries to commit the change to index
0. Only problem is that you removed that item so it applies it to whatever
is at index 0 now, which happens to be your second item (i.e. the one that
previously had index 1). To you this looks like the second item is removed,
while in fact the first item is removed and the second item simply gets its
text changed

Imagine what happens when you edit the last item......
(btw, you can see that the correct item is being removed if you press Esc
instead of Enter)

To work around this use BeginInvoke to asynchronously invoke a method that
removes it, ensuring that it occurs after the event has completed.

private delegate void RemoveItemDelegate(ListViewItem item);
private void RemoveItem(ListViewItem item)
{
item.Remove();
}

private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
ListViewItem item = listView1.Items[e.Item];
listView1.BeginInvoke(new RemoveItemDelegate(RemoveItem), item);
}

/claes

"Kela" <Ke**@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
An interesting problem:
I have a ListView with LabelEdit set to TRUE. When I change the label, I
want to make some decisions as to whether the ListViewItem (that's just
been
edited) should stay in the ListView or not.
The natural place to inspect this is in the AfterLabelEdit event...
If the decision is to take the item out, I used the ListViewItem.Remove().
However - check this out:
A) say that the item's index is N (indices are zero-based)
B) if there exists an item after it (i.e. with index N+1)
then this item gets quietly removed
C) if the item is the last in the list,an exception is thrown
saying that index N (requested to be removed) is non-existant.

It looks like:
D) when the item is being edited, it is taken OUT of the
ListView.Items collection. So - the original item N+1 moves up and
is temporarily assigned index N (that is how it gets removed - see
B) above)
E) if however, item N is the last in the collection and gets
temporarily taken out while being edited, then of course we get
and exception thrown as there isn't an item with index N

This can easily be proved:
1. create a new Windows Forms project
2. add a ListView to the form
3. Set LabelEdit to TRUE
4. Add 3 items to the list, with labels e.g. 'Item1', 'Item2' and 'Item3'
5. Create a handler for the AfterLabelEdit event
6. Inside this handler add this code:

ListViewItem item = listView1.Items[e.Item];
item.Remove();

7. Run the application
8. Edit the first item and press Enter -the second item is removed
9. Next, edit the last item then press Enter -exception is thrown

This is all very nice...

But - can someone suggest an elegant workaround , i.e. - where and
how can I remove items after they have been edited?

Many thanks in advance.


Jul 28 '06 #3

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

Similar topics

6
1509
by: Jules Winfield | last post by:
I'm often in a situation where I have a Hashtable full of objects. I'm then presented with an object and *if* that object exists in the Hashtable, I need to remove it from the table. I therefore...
3
6288
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
0
1339
by: Andres Romero | last post by:
How can I catch the Key event in a treeview before the AfterLabelEdit event to catch the Escape key? My code has both events declared in the treeview, but the first executed is the afterlabeledit...
3
7762
by: bob | last post by:
Hello, the nodes in my tree view show a name and then a suffix. I want to be able to edit the name using 'label edit' but I want the suffix to be removed and then added after the edit. I try...
4
5485
by: Anders Borum | last post by:
Hello! I am working on improving my threading skills and came across a question. When working with the ReaderWriterLock class, I am getting an unhandled exception if I acquire a WriterLock with...
0
1535
by: Graham | last post by:
Hi everyone I have a form which contains a ListView control. The code handles the SelectedIndexChanged event, and changes some icons on some items in the ListView. I've found that when I...
1
1703
by: Alan T | last post by:
I have a listview defined a coloumn at design time. And the code I use to add a listviewitem: ListViewItem lvi = new ListViewItem(user.Name); lvi.Tag = (Object)user.Id; ...
4
3100
by: indrawati.yahya | last post by:
According to the FAQ, the best way to inform a class user of an error that occurs inside a constructor is to throw an exception. My question is, what happens when an object is instantiated using...
10
18043
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
0
7051
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
6915
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
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
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
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
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.