G
Guest
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.
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.