I've got a List and a ListView. The List stores a bunch of information and the ListView displays some of it. I'm trying to add a context menu so that when I select item(s) in the ListView I can either save or remove them. As long as I haven't removed anything, it works great, as soon as I remove something my indexes get messed up. How I'm doing it is doing a foreach loop through the ListView.SelectedIndices and doing List.RemoveAt(index) and ListView.RemoveAt(index).
Is there a way I can remove those items without my indexes getting messed up?
I think basically what's happening is, I select item 1 and 2, indices 0 and 1. It goes through the loop and removes the item at index 0, which makes the item at index 1 turn into index 0, then it removes the item at index 1, which is actually the original item at index 2. So in essence I'm removing items 1 and 3 (indices 0 and 2). I have a feeling if I do it backwards, starting with the largest selected index, I won't have this problem, but I can't seem to find a way to operate in reverse. I tried:
ListView.SelectedIndexCollection selected = fileList.SelectedIndices;
Then doing Array.Reverse(selected), but selected isn't an array. I also tried to see if SelectedIndexCollection had some method to reverse...it doesn't.
Can someone help me? Also, if I'm wrong and doing it in reverse won't help, can you suggest a way I might go about it?