none
Is it possible to use a for loop to find all the items of the same type?

    Question

  • I have numerous PictureBoxes in my program that aren't named yet so are following the default naming pattern. I am wondering if it is possible to use a for loop to find the picture boxes and apply the same code to them to make it so when the sprite that is moving collides with the picture box, it would just make it so the sprite can touch it but not go through it or bounce on it when touching it.

    Thanks :)

    Saturday, March 25, 2017 2:16 PM

Answers

  • i understand the basics of it but I have over 40 picture boxes and don't want to particularly apply lines of code to each individual one.

    Thanks :)

    Hi

    This illustrates looping through all Controls on a Form and finds all Pictureboxes.

            ' loop through all controls on form and if
            ' picturebox, perform code.
            For Each c As Control In Controls
                If c.GetType() Is GetType(PictureBox) Then
                    ' code for each picturebox here
                End If
            Next
    


    Regards Les, Livingston, Scotland

    Saturday, March 25, 2017 6:58 PM

All replies

  • I have numerous PictureBoxes in my program that aren't named yet so are following the default naming pattern. I am wondering if it is possible to use a for loop to find the picture boxes and apply the same code to them to make it so when the sprite that is moving collides with the picture box, it would just make it so the sprite can touch it but not go through it or bounce on it when touching it.

    Thanks :)

    Hi

    Put 'vb.net collision detection' in the search box at the top of this page. There are lots to choose from.


    Regards Les, Livingston, Scotland

    Saturday, March 25, 2017 6:08 PM
  • i understand the basics of it but I have over 40 picture boxes and don't want to particularly apply lines of code to each individual one.

    Thanks :)

    Saturday, March 25, 2017 6:20 PM
  • i understand the basics of it but I have over 40 picture boxes and don't want to particularly apply lines of code to each individual one.

    Thanks :)

    Hi

    This illustrates looping through all Controls on a Form and finds all Pictureboxes.

            ' loop through all controls on form and if
            ' picturebox, perform code.
            For Each c As Control In Controls
                If c.GetType() Is GetType(PictureBox) Then
                    ' code for each picturebox here
                End If
            Next
    


    Regards Les, Livingston, Scotland

    Saturday, March 25, 2017 6:58 PM
  • Thats similar to what I tried to write but sadly didn't work. This did the trick. Thanks a lot!

    Thanks :)

    Saturday, March 25, 2017 7:54 PM
  • i understand the basics of it but I have over 40 picture boxes and don't want to particularly apply lines of code to each individual one.

    If you are using the same code in the same event handler for each of those picture boxes, then you code it once only.  You can connect each picturebox to that code using a loop. There is no need to apply lines of code to each one.

    Searching through all the pictureboxes each time there is a possibility of a collision will take far too long.

    Saturday, March 25, 2017 10:55 PM