0

I need to create 49 PictureBoxes and then place them on a Form. I was thinking in something like

Public Class Form1
   Dim grid() as PictureBox
   Public Sub Form_Load () Handles Me.Load
      For i = 0 to 48
         grid(i) = New PictureBox
         grid(i).Visible = True
         Me.Controls.Add(grid(i))
      Next
   End Sub

Debug console tells me grid(i) = Nothing

4
  • Those are all going to be in the same location Commented Oct 31, 2017 at 0:33
  • I know, but if I manage to create them, spreading them out would be easy Commented Oct 31, 2017 at 0:34
  • Probably easier with a flowlayoutpanel rather than an array Commented Oct 31, 2017 at 0:39
  • Isn't it going to raise an out of range exception? Commented Oct 31, 2017 at 0:41

1 Answer 1

2
Dim grid(0 to 48) As PictureBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        For i = 0 To 48
            grid(i) = New PictureBox
            Me.Controls.Add(grid(i))
        Next
    End Sub

or

 Dim grid(48) As PictureBox
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            For i = 0 To 48
                grid(i) = New PictureBox
                Me.Controls.Add(grid(i))
            Next
        End Sub

or

Dim grid() As PictureBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        redim grid(48)
        For i = 0 To 48
            grid(i) = New PictureBox
            Me.Controls.Add(grid(i))
        Next
    End Sub

if you don't like the limit and have to ReDim your array then use a List.

 Dim grid As List(of PictureBox)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
           grid=new list(of picturebox)
            For i = 0 To 48
                grid.add(New PictureBox)
                Me.Controls.Add(grid.item(grid.count-1))
            Next
        End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

it's not grid(i) that is nothing. it is grid() that is not initialized
How is this any different from what I'm doing? You're just limiting the array size to 49.
you are asking for help. I gave you the answer in the comment on why your code isn't working. I'm not limiting the array. I'm initializing it
@RicRev He's not limiting the array size. He's giving it any size at all. Without the size, all you have is a variable reference that doesn't point to anything. If you're used to a language like javascript, where arrays are dynamic, those aren't real arrays in the formal computer science sense at all. They are collections with some array-like properties. Real arrays have a fixed size. But even this won't work. You also want to set a position for each PictureBox, or they'll all be stacked on top of each other at position (0, 0).
Oh, and VB.Net defines arrays using the index of the last item, not the number of items. So an array with 49 items (indexes from 0 to 48) is declared using a 48 subscript.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.