Removing Items in ListView

C

Curious

Hi,

I am removing problems but I am having a problem. The code below is
the code being used to remove items from the listview.

ListViewItem oldItem = listViewAllStates.SelectedItems[0];
listViewNextStateSelected.Items.Remove(oldItem);

My problem occurs in the following scenario (example):
Initially ListView empty
Add an item
select first item + remove it
add 2 new items
select first item + remove it.......an error occurs.

I also tried with the SelectedIndeces, and same problem occured. What
I noticed was that the selected index is incorrect. In the same
example the index returned was 2.

Can someone help me out
Thanks in Advance
 
W

Wiktor Zychla [C# MVP]

I am removing problems but I am having a problem. The code below is
the code being used to remove items from the listview.

ListViewItem oldItem = listViewAllStates.SelectedItems[0];
listViewNextStateSelected.Items.Remove(oldItem);

My problem occurs in the following scenario (example):
Initially ListView empty
Add an item
select first item + remove it
add 2 new items
select first item + remove it.......an error occurs.

I also tried with the SelectedIndeces, and same problem occured. What
I noticed was that the selected index is incorrect. In the same
example the index returned was 2.

could you post a concise but complete code that reproduces the behaviour you
describe?

Wiktor Zychla
 
C

Curious

Problem solved......I have two listviews, and I was calling the wrong
ListView to get the selected ListViewItem.
 

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