Create Control Array in Visual Baisc 2005 & 2008

Public Class Form1

    Dim MyPictureBoxes() As PictureBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim MyPictureBoxes(29)
        Dim i As Integer
        For i = 0 To UBound(MyPictureBoxes)
            MyPictureBoxes(i) = New PictureBox
            Me.Controls.Add(MyPictureBoxes(i))
            With MyPictureBoxes(i)
                .Visible = False
                .BorderStyle = BorderStyle.Fixed3D
                .Name = "MyPicterbox " & i.ToString
                'U'll have to set the location and other property's...
                'Also if u want the events of the picturenbox to fire u'll need to add an "addhandler" like the one iam using for this example.
                AddHandler MyPictureBoxes(i).MouseClick, AddressOf MyPictureBoxesMouseClick
            End With
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MyPictureBoxes(1).Visible = True
    End Sub

    Private Sub MyPictureBoxesMouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim Pb As PictureBox
        Pb = CType(sender, PictureBox)
        MsgBox(Pb.Name.ToString & " was clicked ")

    End Sub

End Class

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.