Search code examples
vb.netdynamicruntimepicturebox

Adding picture boxes dynamically in VB.Net


I'm sure there is someone out there who can explain this clearly.

I would have assumed that I could simply use:

Dim pb As PictureBox

then I could use

pb = New PictureBox

but apparently not.

I would also assume I can set parameters

pb.Width
pb.Height
pb.Top
pb.Left

etc.

If anyone can help me create a picture box dynamically where I can set the properties I would be so grateful, thanks.

I'm also sorry if this question has already been asked, but I have been looking for a few hours now and nothing has worked for me.


Solution

  • In other words, you are trying to add a control to a windows form programmatically. Of course I don't know what you have tried, but I would use:

    Dim pb As New PictureBox
    pb.Width = 100 'or whatever
    pb.Height = 200
    pb.Top = 50 'or whatever
    pb.Left = 50
    pb.ImageLocation = "dog.png"
    Me.Controls.Add(pb)