创建不止一种PictureBox数组

时间:2019-09-04 10:12:20

标签: arrays vb.net

我正在尝试创建多个PictureBox数组。
我唯一了解的是我可以使用在代码中创建的有限数量的数组。

Dim CustomImage() As PictureBox = New PictureBox() {Array1, Array2,..and so on}

但是我要解释的是如何创建像这样的数组?
在此示例中,用户输入数字6,并显示6个PictureBox。

Dim ArrayNumber as Integer = 6

Dim CustomImage() As PictureBox = New PictureBox() {some code that creates 6 arrays}

1 个答案:

答案 0 :(得分:0)

只是示例:

Dim myPictBox As PictureBox()
Private Const maxPBCount = 10

Private Sub CreateSomePictureBox(ByVal Count As Int16)
    If Count > maxPBCount Then
        Exit Sub
    End If
    Dim myPictBoxLength As Integer = 0
    If myPictBox Is Nothing = False Then
        myPictBoxLength = myPictBox.Length
    End If

    Do While myPictBoxLength > Count
        myPictBox(myPictBoxLength - 1).Dispose()
        myPictBoxLength -= 1
    Loop
    If Count <= 0 Then
        myPictBox = Nothing
        Exit Sub
    End If
    ReDim Preserve myPictBox(Count - 1)
    'Arrange Position of PictureBox
    MsgBox(myPictBoxLength)
    Do While myPictBoxLength < Count
        myPictBox(myPictBoxLength) = New PictureBox
        myPictBox(myPictBoxLength).Height = 30 
        myPictBox(myPictBoxLength).Width = 30
        myPictBox(myPictBoxLength).Location = New Point(35 * myPictBoxLength, 30)
        Me.Controls.Add(myPictBox(myPictBoxLength))
        myPictBox(myPictBoxLength).BackColor = Color.Red
        myPictBox(myPictBoxLength).Show()
        myPictBoxLength += 1
    Loop
    'Create Sub Procedure to Arrange The Picture Box Created 
    'ArrangePB()
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    CreateSomePictureBox(6)
End Sub