我需要创建49个PictureBox,然后将它们放在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
答案 0 :(得分: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
或
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
如果您不喜欢这个限制并且必须重新调整数组,那么请使用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