Removing indices from a List and ListView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Removing indices from a List and ListView

    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.Select edIndices and doing List.RemoveAt(i ndex) and ListView.Remove At(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.Select edIndexCollecti on selected = fileList.Select edIndices;

    Then doing Array.Reverse(s elected), but selected isn't an array. I also tried to see if SelectedIndexCo llection 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?
Working...