Home > DeveloperSection > Forums > How to periodically change background image
ben reitman
ben reitman

Total Post:96

Posted on    October-05-2013 2:47 AM

 VB.Net VB.Net 

 1 Reply(s)
 1690  View(s)
Rate this:

I have 2 forms, frmMain and frmPictures. In frmMain I have a timer whose interval is 5000 (5 seconds). frmPictures has 16 pictureboxes with images already loaded in them. In each timer tick I need to change the frmMain background image.. on startup the background image is the same as picturebox1.

On each timer tick, the program should randomly select a PictureBox in frmPictures and change the background image of frmMain to the image of the selected PictureBox.

How do I do this in VB.NET?

F Scott Fitzgerald
F Scott Fitzgerald

Total Post:29

Posted on    October-05-2013 4:24 AM

Firstly, you should gather all PictureBoxes in an array or similar structure. That can happen e.g. in the Form_Load event:

Dim pictures(15) As PictureBox
pictures(0) = frmPictures.PictureBox1

Btw, why do you have PictureBoxes for each picture? It would be sufficient to load the images at application startup:

Dim pictures(15) As Image
pictures(0) = Image.FromFile("...")

Then in the timer event, create a random number and pick an image:

Dim rnd = CInt(16 * Rnd())
BackgroundImage = pictures(rnd).Image 
BackgroundImage = pictures(rnd)       

Don't want to miss updates? Please click the below button!

Follow MindStick