Creating pictureboxes in VB.Net

Permit2kill

Member
Joined
Jun 25, 2007
Messages
21
Programming Experience
Beginner
Hey, this will probably seem like a really basic question but I'm wondering how exactly you create objects like picture boxes or labels via code. Any help would be appreciated.
I tried googling a few things but didn't have much luck. If this seems like an easy question, just throw some terms at me and I'll google them.

Thanks.
 
Hi,
VB.NET:
        Dim myLabel As New Label
        myLabel.Text = "Hello!"
        Me.Controls.Add(myLabel)

VB.NET:
        Dim myPictureBox As New PictureBox
        myPictureBox.Size = New Size(300, 300)
        myPictureBox.BackgroundImage = Bitmap.FromFile("c:\pic.bmp")
        Me.Controls.Add(myPictureBox)
 
Hi,
a suggestion, you may use Image property rather than BackgroundImage property of the PictureBox and you may change the SizeMode property as well.
 
If you create New PictureBox in code it won't have Name property set.
 
How would I create a unique name every time the code ran.
Basically I have a loop and it'll be creating a number of different picture boxes.
 
Last edited:
VB.NET:
        Dim picBox As PictureBox
        For count = 0 To 5
            picBox = New PictureBox
            picBox.Image = Image.FromFile("")
            picBox.Name = "picBox" & count.ToString()
            Me.Controls.Add(picBox)
        Next
 
also dont forget to give them locations on the form and a size

Dim picBox As PictureBox
For count = 0 To 5
picBox = New PictureBox
picBox.Image = Image.FromFile("")
picBox.Name = "picBox" & count.ToString()
picbox.top = Top Location
picbox.Left = Left Location
picbox.Size = New System.Drawing.Size(50, 25)
Me.Controls.Add(picBox)
Next
 
Back
Top