Search code examples
vb.netwinformspicturebox

Displaying images from a picturebox list


I am trying to display a line of pictures in my program. But I am having a problem, where it is only showing the first image in the imagelist and only showing one image-box.

Private Cards As New List(Of PictureBox)
  Private Sub SetupCards()

    For i As Integer = 0 To imglist1.Images.Count - 1
        Dim PicCard As PictureBox = New PictureBox()
        PicCard.Width = 100
        PicCard.Height = 200
        PicCard.Top = 50
        PicCard.Left = 50
        Me.Controls.Add(PicCard)
        PicCard.Image = imglist1.Images(i)
        Cards.Add(PicCard)
    Next i
  End Sub

Solution

  • You're placing the picture boxes on top of each other, which is why you only see the last card. You've got to set a different Left property for every picture box you add.

    The solution is rather simple. Just add the picture box's width to Left, multiplied by the current index i.

    PicCard.Left = 50 + PicCard.Width * i