3 Replies - 3575 Views - Last Post: 29 September 2012 - 08:34 PM Rate Topic: -----

#1 Coolclone  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 24-September 12

Creating a picture box programmatically

Posted 29 September 2012 - 07:26 PM

Howdy! I'm attempting to create a picture box with my code when a timer ticks. The picture box will also be generated at a random point on the form. I'm not entirely sure how to create a picture box through code, however, so here's my Sub for this.
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim x As Integer = randomGen.Next(0, 772)
        Dim y As Integer = randomGen.Next(0, 470)

        protetNum = protetNum + 1

        Dim picturebox As New PictureBox
        picturebox.Name = "protet" + protetNum.ToString
        picturebox.SizeMode = PictureBoxSizeMode.StretchImage
        picturebox.Image = System.Drawing.Bitmap.FromFile(My.Computer.FileSystem.GetName("jumping alien.gif"))
        picturebox.Location = New Point(x, y) 


Also, my variables
  Dim randomGen As New Random
    Dim protetNum As Integer 


When I run this, the picture box is just not showing up. The file for the image is there, and it can find it, but every time the timer ticks (every 1 second) nothing is happening. I also tested this in a different form:
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim picturebox As New PictureBox
        picturebox.Name = "protet"
        picturebox.SizeMode = PictureBoxSizeMode.StretchImage
        picturebox.Image = System.Drawing.Bitmap.FromFile(My.Computer.FileSystem.GetName("jumping alien.gif"))
        picturebox.Location = New Point(100, 100)
        picturebox.Visible = True
        picturebox.Width = 100
        picturebox.Height = 100
    End Sub



It still doesn't show up, even when I press the button. I honestly don't know what's wrong here.

Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Creating a picture box programmatically

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Creating a picture box programmatically

Posted 29 September 2012 - 07:49 PM

You need to add the picturebox to whatever your container object is. If it's a form, then you need to add it to the control collection.

Form1.Controls.Add(*picturebox control name here*)


Was This Post Helpful? 2
  • +
  • -

#3 Coolclone  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 24-September 12

Re: Creating a picture box programmatically

Posted 29 September 2012 - 08:25 PM

@ trevster344 Thanks! Works perfectly in both now! I love how it's always something so simple.
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Creating a picture box programmatically

Posted 29 September 2012 - 08:34 PM

Haha, no problem man. We've all been there. Take care!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1