There is a code

List<object> tmp = new List<object>(); foreach (object Item in listBoxPlaneType.SelectedItems) tmp.Add(Item); foreach (object Item in tmp) listBoxPlaneType.Items.Remove(Item); tmp.Clear(); 

which very well removes selected items from the listBoxPlaneType listBoxPlaneType

this macrame is outofrange

  List<object> tmp = new List<object>(); List<int> tmp2 = new List<int>(); foreach (object Item in listBoxPlaneType.SelectedItems) tmp.Add(Item); foreach (int Item2 in listBoxPlaneType.SelectedIndices) tmp2.Add(Item2); foreach (object Item in tmp) listBoxPlaneType.Items.Remove(Item); foreach (int Item2 in tmp2) listBoxNumber.Items.RemoveAt(Item2); tmp.Clear(); tmp2.Clear(); 

Which should delete the selected items from the first listbox (which it does) and delete the items from the second listbox to the addresses selected in the first listbox. These two leafboxes Choose a full name and delete, then the number should be removed to the right. Sometimes it does not throw out with an error, but removes the wrong ones from the license plate.

I know that there is a sensitive problem with the circulation on the index (I hope so), but the offset by +1 and -1 does not help.

UPD: Added result result work with an error if I try to remove Il 2 and Outlaw 1

  • And what about debager? You can execute code in steps - Vadim Prokopchuk
  • Well, in the debager it doesn’t come to me how to put stopping points in the forich and get confused in the indices. - elislavkat
  • tmp.Clear(); Do you think GC is dumber than you? - Alex78191
  • 3
    RemoveAt removes by index. After removing one item, the remaining indexes change. - Alexander Petrov
  • one
    You must first get the objects from the second list, and then delete not by the index, but by the object. - Alex78191

2 answers 2

I would do something like this:

 var indices = listBoxPlaneType.SelectedIndices.Cast<int>().Reverse().ToList(); foreach (int i in indices) { listBoxPlaneType.Items.RemoveAt(i); listBoxNumber.Items.RemoveAt(i); } 
  • You can simply Reverse() and in ToList() convert optional. - Alex78191
  • @ Alex78191: Right! Forgot about it, thanks! - VladD
  • @ Alex78191: And I’m sure to convert, I’m sure to, otherwise I’ll remove it from the collection during the iteration. - VladD
  • We use indices simply to get an index iterator. - Alex78191
  • @ Alex78191: Well, yes, but the very first listBoxPlaneType.Items.RemoveAt(i) obviously has to change the listBoxPlaneType.SelectedIndices collection, isn't it? And it is impossible to iterate over a changing collection, it will fly out. - VladD
  List<object> tmp = new List<object>(); List<object> tmp2 = new List<object>(); foreach (object Item in listBoxPlaneType.SelectedItems) tmp.Add(Item); foreach (int Item in listBoxPlaneType.SelectedIndices) tmp2.Add(listBoxNumber.Items[Item]); foreach (object Item in tmp) listBoxPlaneType.Items.Remove(Item); foreach (object Item in tmp2) listBoxNumber.Items.Remove(Item); tmp.Clear(); tmp2.Clear(); 

The code does everything perfectly. The problem, as I was told in the comments, was that by deleting one item by index, the items become smaller, I try to delete item 4, although there are no 4 items in the sheet, and the numbering has also changed. It is necessary to create the 2nd sheet tmp2 not with indices, but with items from the 2nd list box (only those that are highlighted on the left), and delete them by these items.