我正在使用PictureBox控件编写程序。如果我想用PictureBox控件做的事情已经在数组中,那将是最简单的方法。这是我尝试过的。
Sub drawtiles()
For x As Integer = 0 To 32 Step 1
For y As Integer = 0 To 24 Step 1
Dim tile As New PictureBox()
tile.Width = 20
tile.Height = 20
tile.Top = y * 20
tile.Left = x * 20
tile.BackColor = Color.CornflowerBlue
Dim r As Random = New Random
' Get random numbers between 1 and 3.
' ... The values 1 and 2 are possible.
Dim s As Integer = (r.Next(1, 3))
If s = 1 Then
tile.BackgroundImage = My.Resources.g1774
Else
tile.BackgroundImage = My.Resources.rect881
End If
tiles(x)(y) = tile
Controls.Add(tile)
Next
Next
End Sub
...在这里制作数组
Public Class Form1
Public tiles As Array
所以,如何将Picturebox控件分配给3d数组值?谢谢,suryanta
Public tiles() As PictureBox
或此:
Public tiles(32*24) As PictureBox
或者这(可能是您最适合的):
Public tiles(32, 24) As PictureBox
或此:
Public tiles As New List(Of PictureBox)
或完全跳过多余的数组,将控件放入Panel或GroupBox容器中,并在需要查找时编写如下代码:
For Each tile As Picturebox in TilePanel.Controls.OfType(Of PictureBox)
' ...
Next
[请记住,随着迁移到.Net生态系统,当事物类型严格时,VB的运行效果会好得多。 Option Strict
确实应该为On
,并且您需要非常具体的类型名称。
Private jaggedArrays As PictureBox()()
这是一个二维数组:
Private twoDimensionalArray As PictureBox(,)
不同之处在于第二个对象是单个对象,而第一个对象是包含多个1D数组的1D数组。
您应该像这样创建数组:
Private tiles(32, 24) As PictureBox
然后像这样填充它:
For i = 0 To tiles.GetUpperBound(0)
For j = 0 To tiles.GetUpperBound(1)
Dim tile As New PictureBox
'...
tiles(i, j) = tile
Next
Next