請注意,創建Bricks
數組後,此數組仍爲空,即其所有元素均爲Nothing
。您需要將表單上的PictureBox分配給此數組,除非您想反過來動態創建圖片框。
在第一種情況下做這樣的事情:
Bricks(0,0) = pictureBox1
Bricks(0,1) = pictureBox2
...
或用循環
For x As Integer = 0 To 9
For y As Integer = 0 To 9
Bricks(x, y) = Controls("pictureBox" & (10 * x + y))
Next
Next
或者(10 * y + x)
取決於你選擇的順序。
在其他情況下(自動創建的圖片框)
For x As Integer = 0 To 9
For y As Integer = 0 To 9
'Create and initialize the PictureBox
Dim pb AS New PictureBox
pb.Size = New Size(30, 30)
pb.Location = New Point(20 + 30 * x, 20 + 30 * y)
pb.Image = myinitialImage
AddHandler pb.Click, AddressOf HandlePictureClick
'Assign the PictureBox to the array
Bricks(x, y) = pb
'Add the PictureBox to the Form
Controls.Add(pb)
Next
Next
現在你可以分配這樣
Bricks(x, y).Image = myPicture
圖片[在Visual Basic ** **陣列(HTTPS:/ /msdn.microsoft.com/en-us/library/wak0wfyt.aspx)和[** For Each ... Next statement **](https://msdn.microsoft.com/zh-cn/library/5ebk1751。 aspx) –