PictureBox array


  • I have just begun to work with VB2005 after many years of hanging in there with VB 3.

    I’m trying to write a card game where two decks, or 104 cards all appear face up at the start of the game.

    In VB 3 I created a picture box array by naming two with the same name, which was “Card”. Not very elegant, but that was how VB 3 did it.

    Then I only needed the code

    For I = 2 to 104

                Load Card(I%)

    Next I


    How is this simple task done in VB2005?
    • Edited by originaltyro Wednesday, December 21, 2011 2:28 AM
    Wednesday, December 21, 2011 2:26 AM

All replies

  • Arrays work pretty much the same in .Net as they did in VB3.

    You can do
        Dim Card(104) as PictureBox
    to create an array that can contain 104 pictureboxes.  You need to create the pictureboxes in code and set their properties, but I would guess that your VB3 program was already doing that, as you wouldn't want to lay out 104 pictureboxes by hand.

    However, that design would be very inefficient, as you probably don't need that many pictureboxes.  It's more likely that you would use an array of 104 images, or possibly an ImageList control with 104 images, and display the images laid out in one picturebox. Manipulating the size, position and display state of an image is much the same as for a picturebox, without all the overhead of a control.

    • Edited by AcamarMVP Wednesday, December 21, 2011 3:24 AM sp
    Wednesday, December 21, 2011 3:23 AM
  • Like Acamar wrote you can use the same array (the only difference is there is (luckily) not anymore a designer feature for that)

    However, it seems Acamar is not so often playing cards. 

    You can create all those pictureboxes still with the designer, but if you want them in an array you have to put them in an array for instance in the load event. 

        Private PictureBoxArray As New ArrayList
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each ctr as Control In Controls
                If TypeOf ctr Is PictureBox Then
                End If
        End Sub


    You can then create an array of your pictureboxes which are all on the surface of your form (not in a panel of groupbox) use this code.

    However, why are you using VB2005 (language version 8). VB10SP1 is freeware and in my perception does full VB2005 add nothing to your project you cannot do with VB2010 Express. (Beside the deployment but for that you can then use VB2005).

    If you use VB2010 with its automatic properties and initializers your code can look like this.


    Public Class Form1
        Private Cards As New List(Of card)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each ctr As Control In Controls
                If TypeOf ctr Is PictureBox Then
                    Cards.Add(New Card With {.CardPosition = ctr.Tag})
                End If
        End Sub
        Public Class Card
            Public Property CardType As Integer
            Public Property DeskPlace As Integer
            Public Property FaceUp As Boolean
            Public Property CardPosition As Object
        End Class
    End Class


    It implies that you set in the tag property in the designer your CardPosition.

    Don't be afraid of this code, while doing it you see it is easy.




    Wednesday, December 21, 2011 8:51 AM
  • Cor Lighert, I tried your code and I must conclude that you granted me more knowledge than I have.

     I stepped through the code and found that it only looped twice which I suspect is the default for an array.

     I tried to change:

        Private PictureBoxArray As New ArrayList


        Private PictureBoxArray(104) As New ArrayList
    …but I was told that “New” could not be used in the statement.

    So I took out “New” only to get an error message and a hint to “add the keyword New”.


    Meanwhile, in my effort to understand all of the new lingo, I stumbled across this definition of class...

    “A reference type that encapsulates data (constants and fields) and behavior (methods, properties, indexers, events, operators, instance constructors, static constructors, and destructors), and can contain nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.”

    …and had a brief moment wherein I thought I should go back to VB 3. However I have persevered.


    Also, I downloaded what I thought was vb2010 (it was offered in the same page as Studio 2010 as VB Express) but it starts up calling itself “Visual Basic 2008 Express edition”.

     Still, a step forward.

    I stepped myself through the tutorial on the Basic language and learned a few things (however, I will continue to use “Call” in front of a subroutine call and prefix my function names with “Fn” and use % to identify an integer - and all of those other characters for other variable types - because all of these things help to make the code readable).

    But I digress.

    Thank you for your help. And if there is something in all of this that prompts you to add some further assistance, that would be appreciated.
    Thursday, December 22, 2011 9:38 PM