尝试将图片分配给 VB 中的 PictureBoxes
Trying to assign pictures to PictureBoxes in VB
我正在尝试创建一个简单的游戏,首先它需要随机加载 16 个带有图像的 PictureBoxes。不知道问题出在哪里。
Public Class Form1
Private picArrows() As PictureBox = {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'starts a new game
'declares RNG
Dim randGen As New Random
'uses RNG to determine arrow placement
For intPicBox As Integer = 0 To 15
Select Case randGen.Next(1, 5)
Case 1
picArrows(intPicBox).Image = My.Resources.Up
Case 2
picArrows(intPicBox).Image = My.Resources.Right
Case 3
picArrows(intPicBox).Image = My.Resources.Down
Case 4
picArrows(intPicBox).Image = My.Resources.Left
End Select
Next
End Sub
End Class
我在案例 X 之后的行中收到 NullReferenceException 错误。有人知道我做错了什么吗?
I get a NullReferenceException error on the line after Case X
您不能像这样初始化数组:
Public Class Form1
Private picArrows() As PictureBox = {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
窗体还没有初始化,所以它和它上面的所有控件都还没有创建。结果,所有这些控件引用都将是 Nothing
,留下一个充满 Nothing
的数组。结果是 NullReferenceException
因为 Nothing
没有 Image
属性.
您可以在那里声明数组,但是您只能在表单的构造函数运行后初始化它(Sub New
) . Form Load 是个好地方:
Public Class Form1
Private picArrows As PictureBox()
' for best results you should use the same RNG over and over too:
Private randGen As New Random()
...
Private Sub Form_Load(....
picArrows = New PictureBox() {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
另见 NullReference Exception in Visual Basic
没有伴随数组的排列略有不同:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
With New Random
For col = 1 To 4
For row = 1 To 4
CType(Controls(String.Format("pic{0}{1}", col, row)), PictureBox).Image = {My.Resources.Up, My.Resources.Right, My.Resources.Down, My.Resources.Left}(.Next(0, 4))
Next
Next
End With
End Sub
我正在尝试创建一个简单的游戏,首先它需要随机加载 16 个带有图像的 PictureBoxes。不知道问题出在哪里。
Public Class Form1
Private picArrows() As PictureBox = {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'starts a new game
'declares RNG
Dim randGen As New Random
'uses RNG to determine arrow placement
For intPicBox As Integer = 0 To 15
Select Case randGen.Next(1, 5)
Case 1
picArrows(intPicBox).Image = My.Resources.Up
Case 2
picArrows(intPicBox).Image = My.Resources.Right
Case 3
picArrows(intPicBox).Image = My.Resources.Down
Case 4
picArrows(intPicBox).Image = My.Resources.Left
End Select
Next
End Sub
End Class
我在案例 X 之后的行中收到 NullReferenceException 错误。有人知道我做错了什么吗?
I get a NullReferenceException error on the line after Case X
您不能像这样初始化数组:
Public Class Form1
Private picArrows() As PictureBox = {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
窗体还没有初始化,所以它和它上面的所有控件都还没有创建。结果,所有这些控件引用都将是 Nothing
,留下一个充满 Nothing
的数组。结果是 NullReferenceException
因为 Nothing
没有 Image
属性.
您可以在那里声明数组,但是您只能在表单的构造函数运行后初始化它(Sub New
) . Form Load 是个好地方:
Public Class Form1
Private picArrows As PictureBox()
' for best results you should use the same RNG over and over too:
Private randGen As New Random()
...
Private Sub Form_Load(....
picArrows = New PictureBox() {pic11, pic12, pic13, pic14,
pic21, pic22, pic23, pic24,
pic31, pic32, pic33, pic34,
pic41, pic42, pic43, pic44}
另见 NullReference Exception in Visual Basic
没有伴随数组的排列略有不同:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
With New Random
For col = 1 To 4
For row = 1 To 4
CType(Controls(String.Format("pic{0}{1}", col, row)), PictureBox).Image = {My.Resources.Up, My.Resources.Right, My.Resources.Down, My.Resources.Left}(.Next(0, 4))
Next
Next
End With
End Sub