Autoredraw for Picturebox

JTBob

Member
Joined
Feb 24, 2006
Messages
8
Programming Experience
10+
Hi,

I migrated a program from VB6 to VB.Net 2005 and have a couple of issues still to resolve.

The first is getting a Picturebox to redraw when the form comes back up. My program have two open forms. The primary window has all incoming data, such as GPS information. The secondary window has the Picturebox and shows the graphic display of sightings in relation to the sighting platform.

All graphics to the Picturebox are handled by a few subroutines in a module. If I just go back to the form, the Picturebox is blank. I have to click a scale button or wait for another GPS input for the image to redraw. I'd like it to happen as soon as the form is selected.

I tried placing the call to the graphics subroutine in the Picturebox_Paint event, but it still doesn't work. It looks like for a split second that the image is redrawn, but then goes blank again. It's as if the call to the graphics subroutine is happening before the Picturebox is fully visible again.

Anyone have any ideas on what I can try? I've exhausted my ideas.

Thanks,
Robert
 
You try to 'steal' the PictureBox by using CreateGraphics. Instead either draw to an bitmap/image and let the PictureBox display it, or use the Graphics object provided by its Paint event to draw with. For the latter you will find it in e.Graphics and you can pass this instance to any method you call to have the work done from the event handler.
 
Hi John,

Thanks for the reply. You are correct that I'm using CreateGraphics in my UpdateGraph subroutine. A couple of questions come to mind in using the e.Graphics for the Paint event of the Picturebox:

How would I go about passing the instance of the graphics handler to my subroutine? I've got a couple of ideas of how it would work, but without reference material, it's still a little fuzzy.

Also, what causes the Paint event to fire? Can this event be fired when I need it to occur, such as through a button press or from new data from the GPS? If not, could I still use the e.Graphics class or would I have to revert back to CreateGraphics?

Sorry for the newbie questions. It's been a slow learning process with trial-and-error and having to change my thinking from VB6.

Thanks,
Robert
 
examples:
VB.NET:
Private Sub ButtonRefresh_Click(sender, e) Handles ButtonRefresh.Click
  PictureBox4.Refresh()
End Sub
 
Private Sub PictureBox4_Paint(sender, e) Handles PictureBox4.Paint
  myCustomPaint(e.Graphics)
End Sub
 
Private Sub myCustomPaint(ByVal g As Graphics)
  'g.drawSomething
End Sub
If it's very graphics intensive you can instead of Picbox.Refresh use one of the PixBox.Invalidate overloads to only refresh a region you have repainted. Check the docs.
 
Hi John,

Thanks a lot for the examples. Everything worked perfectly. Passed the instance to the necessary subroutines and commented out the CreateGraphics references. Definitely gives me a clearer picture of what's going on in the background.

Thanks again,
Robert
 
Back
Top