'Drawing an array of PictureBoxes in vb.net

I'm trying to draw an array of PictureBoxes, for testing I use the same picture for each picturebox.

But instead of showing the picture, it shows the color blue.

I would show you a picture, but I dont have 10 reputation..

        Dim teren(120) As PictureBox
        Dim x_locatie As Integer = 1, y_locatie As Integer = 0
        For i = 0 To 10
        x_locatie = 210
        For j = 0 To 12
            teren(i * j) = New PictureBox()
            teren(i * j).Size = New Size(61, 61)
            teren(i * j).Name = "x" + i.ToString + "y" + j.ToString
            teren(i * j).Location = New Point(x_locatie, y_locatie)
            Dim locatie As String = folder + "\harta\test.png"
            teren(i * j).ImageLocation = locatie
            teren(i * j).Show()
        Next
        y_locatie += 61
    Next

I also tried another method , but same result.

Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs)
    If myImage IsNot Nothing Then
        Dim r As New Rectangle(x, y, xlatime, ylungime)
        er.Graphics.DrawImage(myImage, r)
    End If
End Sub

Sub deseneaza(ByVal poza As String, ByRef x_perm As Integer, ByRef y_perm As Integer, ByRef lungime As Integer, ByRef latime As Integer)
    myImage = Image.FromFile(poza)
    x = x_perm
    y = y_perm
    xlatime = latime
    ylungime = lungime
    Refresh()
End Sub

 'this part of code is in body of another function
 Dim x_locatie As Integer = 1, y_locatie As Integer = 0
        For i = 0 To 10
        x_locatie = 210
        For j = 0 To 12
            Dim locatie As String = folder + "\harta\test.png"
            deseneaza(locatie, x_locatie, y_locatie, 61, 61)
        Next
        y_locatie += 61
    Next

I saw in other threads that their problem solution was something like that Dim teren() As PictureBox {teren1, teren2 , ... , teren n} But the problem in my case is that I need 120 PictureBoxes, and I think that it must be a way to do this without writing 120 pictureboxes.



Solution 1:[1]

Please try this...it will generate 16 picture box, size 20x20, in a row. I put it under "FormLoading" event.

        Dim Shapes(16) As PictureBox
        For i = 1 To 16
            Shapes(i) = New PictureBox
            With Shapes(i)
                .BackColor = SystemColors.Control 'Color.Green
                .BackgroundImage = New Bitmap(My.Resources.led_blk)
                .BackgroundImageLayout = ImageLayout.Zoom
                .Size = New Size(20, 20)
                .Visible = True
                .Location = New Point( 23 * i, 50)
            End With
            Me.Controls.Add(Shapes(i))
      Next

Solution 2:[2]

Dim iTop = 325
Dim pBox(48) As PictureBox
Dim pinColor = Color.SkyBlue
Dim leftStart = 50
For j = 0 To 3
    For i = 0 To 11
        pBox(i) = New PictureBox
        'pBox(i).Image = Image.FromFile("\NoTest.bmp")
        pBox(i).Visible = True
        pBox(i).BackColor = pinColor
        pBox(i).Top = iTop + (j * 40)


        pBox(i).Width = 20
        pBox(i).Height = 20
        pBox(i).Left = leftStart + (35 * i)
        If i > 9 Then
            pBox(i).Left = leftStart + (35 * i) + 15
            pBox(i).Width = 25
        End If

        pBox(i).BringToFront()
        pBox(i).SizeMode = PictureBoxSizeMode.StretchImage
        Controls.Add(pBox(i))
    Next
Next

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 supwat
Solution 2 Peter Csala