1

I tried many times to resolve this case but I always get an error because it's an Image in an ImageList. What code do I need to literally re-add the removed Image from the list. This is my code (The final line doesn't work).

int index9 = random.Next(0, normalCards1.Count - 1);
pictureBox9.Image = normalCards1[index9];
normalCards1.RemoveAt(index9);
...
normalCards1.Insert(index9);
KoKoL
  • 53
  • 3
  • 9
  • "doesn't work" is bad explanation. Since you should be getting compile error you should add it to the post. – Alexei Levenkov Aug 07 '14 at 15:10
  • Side note: Assuming you trying to write deck shuffling - check out implemetations provided in http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp/1262619#1262619 – Alexei Levenkov Aug 07 '14 at 15:12
  • Please also clarify what `ImageList.RemoveAt` refers to because `ImageList` class does not have `RemoveAt` method. – Alexei Levenkov Aug 07 '14 at 15:25

2 Answers2

6

you need to pass T item as well with index.

you can add it back this way:

normalCards1.Insert(index9,pictureBox9.Image);

See List.Insert Method MSDN docs here

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

ImageListCollection which is the type of ImageList.Images does not provide a way to insert items by index.

If you want to shuffle or somehow else reorder images you need to remove them all and add them again after reordering. I.e. adding all images to a List<T>, sort, and use AddRange.

You can also try using indexed access (imageList.Images[3] = ... ) to swap items.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179