Question How to detect collision with generated pictureboxes

MrRobot

Member
Joined
Sep 20, 2019
Messages
5
Programming Experience
1-3
Hey! I'm new around here, not sure if I'm doing it right but I need some help solving a problem here pretty please.

So basically I have a character which I can control, and I have multiple pictureboxes being generated randomly. My question is, how do I make a collision for all of them?!

Everytime I move the Character.Bounds code away from where the Picturebox are being generated, it says my Picturebox has no context. I know I have to generate something with it but I don't know what and where... can you help me?

I'll use images since I don't know how to make the code look fancy on the foruns yet, sorry :p

codeHelp.png
 
Does that really work? Normally, you'll run into issues when you remove items

If it works for your particular situation, it looks like you lucked out.

Yes, it's working as intended and not a single problem so far. Each individual picturebox disappears with the respective click. I even set a max of Seeds the game can spawn so it makes easier for me, and as I remove 1 it opens up a space for another to spawn so I guess it's working fine. :D

gif.gif
 
It's still not recommended to edit a collection in iteration. Consider using the safer versions as demonstrated on post 14, 15. Should be easy to tweak and remove the possibility of an exception arising later in your code.
 
Very interesting to find this in the .NET Framework reference source. Unlike the other collections in the .NET Framework, the Control.ControlCollection's enumerator seems to support having the collection modified while iterating as the result of a bugfix for Whidbey bug #448276:
C#:
// VSWhidbey 448276
// We have to use Controls.Count here because someone could have deleted
// an item from the array.
//
// this can happen if someone does:
//     foreach (Control c in Controls) { c.Dispose(); }
//
// We also dont want to iterate past the original size of the collection
//
// this can happen if someone does
//     foreach (Control c in Controls) { c.Controls.Add(new Label()); }
 
Back
Top Bottom