问题
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
回答1:
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
回答2:
Don't need to keep the imaging controls in your own list if you add them to a parent container control.
Use ListView or third-party controls, or use custom drawing code if you need to use ListBox (which wraps respective Windows control). See C# Can I display images in a list box?
来源:https://stackoverflow.com/questions/48681708/displaying-images-from-a-picturebox-list