如何在Visual Basic中创建PictureBox的数组?
我试图制作一排PictureBoxes - 它们的大小和图片相同 - 在整个表单中显示。我怎么能这样做?
我用这个制作了数组:
Dim blk(10) As PictureBox
和放置PictureBoxes的代码是:
'Create PictureBoxes
blk(0) = blk_Green()
blk(0).Image = imgl_blk.Images(0)
blk(0).Visible = True
blk(0).SetBounds(10, 10, blk_Green.Width, blk_Green.Height)
For i = 1 To 10 Step 1
blk(i) = blk_Green()
blk(i).Image = imgl_blk.Images(0)
blk(i).Visible = True
blk(i).SetBounds(i * 10, 10, blk_Green.Width, blk_Green.Height)'I will change this according to what it needs to be
Next
imgl_blk 是一个ImageList,而 blk_Green 是我已在表单上制作的ImageBox。
当我运行它时,只会显示一个PictureBox,而不是数组中的所有10个。
这就是我想要获得的(或类似的东西):
这就是发生的事情:
我怎么能让这个工作?提前谢谢!
答案 0 :(得分:3)
如果要创建控件数组:
Dim blk() As PictureBox
blk = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
然后你可以用blk(0)引用PictureBox1,用blk(1)引用PictureBox2,用blk(2)引用PictureBox3,用blk(3)引用PictureBox4。
答案 1 :(得分:1)
您为所有10个PictureBox分配了相同的Object
引用。您需要分别实例化每个PictureBox,然后分配每个Image
属性。
答案 2 :(得分:0)
你可以试试这个:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Shapes(10) As PictureBox
For i = 1 To 10
Shapes(i) = New PictureBox
Shapes(i).Name = "rect" + Str(i)
Shapes(i).BackColor = Color.Green
Shapes(i).Location = New Point(10 + 50 * i, 20)
Shapes(i).Size = New Size(40, 20)
Shapes(i).Visible = True
Me.Controls.Add(Shapes(i))
Next
End Sub
End Class