Note: This already works fine, but I'm trying to understand Why it works this way, but not the other.
I have a WinForm (C#) with dynamically put images, like so:
Now if you click the 'Napred' button, these images should be deleted (among other things), for which I originally used:
foreach(Control ctrl in Controls)
if(ctrl is PictureBox) ctrl.Dispose();
or
for(int i = 0; i < Controls.Count; i++)
if(Controls[i] is PictureBox) Controls[i].Dispose();
Now if I run this, I get:
But if I just change the for
statement to send it backwards, it works?
for(int i = Controls.Count - 1; i >= 0; i--)
if(Controls[i] is PictureBox) Controls[i].Dispose();
(I'm not going to upload another image, but it deletes all of the elements (I only get the buttons left in the end))
Can someone enlighten me why one works, but not the other?
EDIT: I'm using VS2015 community edition on Windows 10 if it's a debugging error(?)