Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have the following code in the paint event of a picturebox. Would someone please explain why the g.Dispose line raises an exception?

 

Dim g As Graphics = e.Graphics

g.DrawRectangle(Pens.Blue, New Rectangle(0, 0, 10, 10))

g.Dispose()

  • *Experts*
Posted

In the Paint event, you're given the Graphics object. In that case, you shouldn't Dispose of it since there might be other event handlers that are counting on it.

 

Only call Dispose on a Graphics object if you create one (outside of the Paint event usually).

 

I'm not sure why it throws an exception, but don't do it and it will go away :)

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...