Creating Forms with PictureBoxes (at runtime) and changing their images

ozonic

Member
Joined
Jun 17, 2006
Messages
21
Programming Experience
Beginner
How do I create a completely new Form with its own PictureBox spread across it every time I click a button (at runtime), and then be able to change the Image in any of these PictureBoxe at a later stage?
 
VB.NET:
Expand Collapse Copy
        Dim form2 As New Form
        Dim PB As New PictureBox
        Dim x As String = [COLOR=Red][B]FILELOCATION[/B][/COLOR]
        form2.Show()

        With PB

            PB.Image = PB.Image.FromFile(x)
            PB.Height = PB.Image.Height
            PB.Width = PB.Image.Width
            PB.Left = 0
            PB.Top = 0
        End With

        form2.Height = PB.Image.Height
        form2.Width = PB.Image.Width

        form2.Controls.Add(PB)
Set the file location to the image you want, then put this code in a button click or something and try it out :)
 
In this case it would be better to create a form "template" that already had a picturebox with DockStyle=Fill and a property (PBimage) to set the image. Through the property Set you would also if you wish resize the form. So you could do this:
VB.NET:
Expand Collapse Copy
Dim frm As New PBForm
frm.PBimage = Image.FromFile(filepath)
frm.Show()
 
Thanks guys, it works. But how can I access (change) a specific image on a specific form that's been created.

For example: I've clicked the button 5 times and now I have 5 forms: each with its own PictureBox and image loaded into it. Now I decide that want to change the the image on one of those forms; lets say the third form that was created. How can I do that?
 
As I said in last post, you can add a property in the form for this. This property would probably look something like this:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] PBimage() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Image
[/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff] Return[/COLOR][/SIZE][SIZE=2] PictureBox1.Image
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Image)
 PictureBox1.Image = Value
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE]
 
Back
Top