ListView exception

Y

yevron2

Hello,

When i am trying to remove the last item in a listview:

lstServers.Items.Remove(item);

i get the exception:

A first chance exception of type 'System.ArgumentOutOfRangeException'
occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of
valid values.

Does anyone know why that is?

Thanx,
Yael.
 
G

Guest

What is the type of "item"? Is it an integer?

You get this error if you try to access an item with an index that is larger
then the iondex of the last item, for example if you have 5 items in the
listbox then they have indexes 0 to 4, and you will get this error if try to
acces an item using the value 5 as its index.

To remove the last item, try

lstServer.Items.remove(lstServer.Items.Count-1)

Joris
 
Y

yevron2

item is of type ListViewItem.

The item is taken from the selectedItems, and the list view does remove
it but it throws the exception.
 
Y

yevron2

I happens when i do it from a pop up menu that i manually poped at the
lstServers_MouseDown.

Thanx for all your help!
 
R

rhaazy

A first chance exception of type 'System.ArgumentOutOfRangeException'
The item is taken from the selectedItems, and the list view does remove
it but it throws the exception.

It sounds like you have some kind of recurssive behavior going on that
is causing it to try and remove it the first time but fails and when it
tries again it does it, or even vice versa...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top