How to target a group of pictureboxes (Car1 Car2 Car3)


  • I have a bunch of cars going up and down a street and I'm calling them Car1 Car2 Car3 so on

    When the Character (picturebox) touches any of the cars they lose

    Is it possible to make something like "CarGroup = Car1,Car2,Car3"

    OR something like "E = 1 through 8" then combining E with Car to make Car1 and Car2 and Car3

    if (Character.Bounds.IntersectsWith(Car1.Bounds) || Char.Bounds.IntersectsWith(Car2.Bounds))

    Basically trying to avoid that above...

    ...and trying to do something like this below

    if (Character.Bounds.IntersectsWith("Car" + "Var E".Bounds))

    Thursday, November 26, 2015 4:03 PM


All replies

  • If you don't want/need to build an index you can use a search approach like this, provided you have named all your controls uniquely enough according to some consistent scheme like "Car1", "Car2", etc.

    if(Character.Bounds.IntersectsWith(Controls.Find("Car"+yourIndex.ToString()), false)[0].Bounds))

    EDIT: Find returns an array, so access first element [0]
    • Edited by Wyck Thursday, November 26, 2015 10:47 PM Find returns an array, so access first element [0]
    Thursday, November 26, 2015 5:47 PM

    I'm getting an error with bounds

    Thursday, November 26, 2015 6:50 PM
  • Sorry, my bad. Find() returns an array.

    so change




    Thursday, November 26, 2015 10:46 PM