如何使用循环创建 PictureBox 数组?

How do I create an array of PictureBox using a loop?

我需要创建 49 个 PictureBoxes,然后将它们放在一个 Form 上。 我在想

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

调试控制台告诉我 grid(i) = Nothing

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

 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

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

如果您不喜欢这个限制并且必须重新调整您的数组,那么请使用列表。

 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