Updating PictureBox image every x seconds --how?

J

Jerry West

I have a routine that updates a PictureBox image every x seconds. I do this
by first loading an array with the path to all of the images. I then
generate a random number to use as the index of the array. A timer is set to
update the PictureBox. This works great the first time through but fails on
all subsequent Timer events. If I simply step through the code it works
fine. If I run it only the first time through does the PictureBox update the
picture. Can someone tell me what I'm doing wrong in getting all subsequent
calls to update the PictureBox to work? My code is:

Within the Timer Tick event....

Dim Photo As Integer

Photo = RandomNumber(ss.Pics.Count)

Dim myImage As Bitmap = New Bitmap(ss.Pics(Photo).FullName, True)

Me.pic1.Image = myImage

As I said, fine the first time through and then each subsequent Tick event
fails to update the PictureBox w/new pic. I have verified that each time
through the path statement is correct. What the heck? It seems simple
enough. What am I doing wrong? I've tried placing a pic1.Dispose call before
and after updating the PictureBox to no avail.

Any insight appreciated!!

JW
 
M

Mike McIntyre

After you change the image try a call to:

Me.pic1.Invalidate

to force the PictureBox to redraw itself.
 
J

Jerry West

I'm using VB .NET 2005. The PictureBox control doesn't have an Invalidate
method. It does have a Redraw method and I have tried using that after
setting the image --to no avail.

What could I be missing here? This is very simple to do in VB6.

JW
 
C

Cor Ligthert[MVP]

Jerry,

Did you try to disable the timer events at the start of the timer event and
to enable it again at the beginning. Often the event is throwed again even
before the next one is completely handled.


However, I would first try to set the picture number in another way, just to
try it.

Cor
 
J

Jerry West

Cor,

That was the trick!!! I disabled the Timer and re-enabled per your
suggestion and immediately it worked as I needed. I find this somewhat odd
though, given that I had set the Timer value for >10 seconds which should
have been enough to allow the process to work.

Your the man!

JW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top