Results 1 to 9 of 9

Thread: Looping through Form objects

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Looping through Form objects

    I have some code that I want to apply to many objects (Pictre Boxes) and I was unable
    to loop through the objects via a For loop and am currently forced to copy paste the code for each Image Box.

    The Pictre Boxes are named compAlien1, compAlien2 ... compAlien9

    Is there a way to do something similar to this:
    Code:
    For i = 1 To 10 Step 1
    compAlien[i]... #code#
    Next i
    thanks in advance
    Last edited by Acidic33; Feb 5th, 2011 at 03:14 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through Form objects

    The proper name for the control is a PictureBox. It may seem like a small thing but it's important to use accurate terminology to avoid confusion. It seems fairly obvious what you mean on this occasion but that may not always be the case, e.g. the large number of people who say DataGrid when they mean DataGridView, which are similar controls but different so advice for one is not relevant for the other.

    As for the question, there absolutely is a way to do exactly what you posted, and it's pretty much exactly what you posted. The thing is, 'compAlien' has to exist for you to be able to loop through it, so that's what you need to do: create the array to loop through. One option is like this:
    vb.net Code:
    1. Dim compAlient As PictureBox() = New PictureBox() {Me.PictureBox1, Me.PictureBox2, Me.PictureBox3}
    That's always going to work, but is a bit tedious if you have a large number of controls. Another option would be:
    vb.net Code:
    1. Dim compAlien = Me.Controls.OfType(Of PictureBox).ToArray()
    That requires .NET 3.5 or later and will only work if you want all the PictureBoxes and they are all in the same container. You can handle other cases but the code becomes more complex.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through Form objects

    By the way, indexing using brackets is a C thing. In VB, indexing arrays and collections is done using parentheses.

    Also, arrays and collections are zero-based in .NET so you would have to start at index 0, not 1, when using a For loop. If you don't use the index for anything besides getting the element/item then it's neater to use a For Each loop rather than a For loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Looping through Form objects

    I was indeed referring to Picture Boxes, thanks for that.

    I created a Global variable like this:
    Code:
    Dim compAlien As PictureBox() = New PictureBox() {Me.compAlien1, Me.compAlien2, Me.compAlien3)
    To make things simple, i just included three Picture Boxes

    As far as I understand, this equates to (not in text):
    compAlien(0) = compAlien1
    compAlien(1) = compAlien2
    compAlien(2) = compAlien3

    The following line currently calls for NullReferenceException was unhandled:
    Code:
     If laser.Bounds.IntersectsWith(compAlien(i).Bounds) Then
    Last edited by Acidic33; Feb 5th, 2011 at 03:32 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through Form objects

    If, by "global variable", you mean "member variable", i.e. a variable declared outside of any method but inside the form, then that would be expected. The problem is that that member variable is created, and therefore initialised, before any of the form's controls are created. As such, each of your PictureBox variables is Nothing at the time you execute that code. There are two possible solutions:

    1. Instead of declaring a member variable, declare a local variable in the method in which the array is used.

    This means that the array may have to be created more than once, but it's a very cheap operation so that's no big deal. It's not a great idea if the array needs to be used in multiple methods though, because you'd have to repeat the code in each one.

    2. Don't initialise the array until after the controls have been created, i.e. create the array and assign it to the member variable inside the form's Load event handler.

    The code is slightly more involved that way but it does mean that the array is available everywhere within the form code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through Form objects

    By the way, if a variable stores multiple objects, like an array or collection, then it's more appropriate for the name to be pluralised, i.e. use 'compAliens' rather than 'compAlien'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Looping through Form objects

    Yes, I initially declared the array outside any method, inside the Public Class gameMain.
    (my form's Name is gameMain, obviously)

    Regarding Method #1: The code that I want to use will be used inside a timer that currently has an Interval of 20.
    Creating the array 50 times per second for no reason does not sound very efficient to me...

    Regarding Method #2: I declared the array inside Private Sub gameMain_Load...
    but in this case using the array inside Private gameTimer_Tick results in "Name compAlien is not declared."

    any idea what I am doing wrong?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through Form objects

    You need to declare the variable at the class level, so athat it's accessible anywhere within the class, but you need to create the array, and assign it to the variable, inside the Load event handler, e.g.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private pictureBoxes As PictureBox()
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.pictureBoxes = New PictureBox() {Me.PictureBox1, Me.PictureBox2}
    7.     End Sub
    8.  
    9. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Looping through Form objects

    Excellent, that worked perfectly.

    Thanks a lot.

    I'll be sure to rate this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width