Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

We have a picture box on a form with some lines drawn using the Paint() method. However, whenever a message box pops up on top of the picturebox, that portion of the picturebox is erased. The same thing happens if we move the form off the screen, the portion that is off the screen will get erased.

 

We are trying to make a simple graph. We can make it work with a picturebox if we can prevent it from being erased. Using the PictureBox1.Invalidate() in the Paint() method refreshes it so much that it slows down the application. We could use a timer, but as you can imagine that would still look dumb.

 

What is the best way to make a simple graph like this and make it stay?

  • *Experts*
Posted

Do you mean you're doing your drawing in PictureBox_Paint() event?

If that is what you mean, then it should work. Otherwise, that is where you should do all your drawing. Post your code, please. :)

 

[edit]Also, don't use Invalidate inside the Paint() event, because that will cause it to CALL the Paint event; this can cause stack overflow, I think.[/edit]

Posted (edited)

Sample Code

 

Here is the code in our Paint Method. It works, so long as nothing like a message box covers it up. If one does, everything beneath the message box is erased.

 

   Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

       Dim x As Integer
       Dim y As Integer
       Dim MyGDISurface As Graphics = Me.PictureBox1.CreateGraphics
       Dim myWhiteBrush As New SolidBrush(Color.White)
       Dim myRectangle As New Rectangle(1, 1, 272, 152)
       Dim myPen As New Pen(Color.LightGray, 1)
       MyGDISurface.FillRectangle(myWhiteBrush, myRectangle)
       x = 0
       y = 0
       For x = 10 To myRectangle.Width Step 10
           MyGDISurface.DrawLine(myPen, x, 1, x, 152)
       Next
       For y = 10 To myRectangle.Height Step 10
           MyGDISurface.DrawLine(myPen, 1, y, 272, y)
       Next
   End Sub

Edited by divil
  • *Experts*
Posted

That is very odd. When I run your code, and I move a MessageBox

over top of it, it simply causes it to reupdate itself; it just calls the

Paint event again.

  • *Experts*
Posted

All I did was create a new project, put a PictureBox on it and paste

in your code. Nothing more. I don't see the point in sending out the

project.

Posted
Well, I tried making a project with just that code and a button that brings up a message box. Everything that was drawn and was under the message box got erased. I'll keep messing with it I guess. Thanks for the suggestions though!
  • *Gurus*
Posted
Looking at your code, you should not have been creating a graphics object yourself. The Paint event for any control gives you the correct graphics object to use as one of the properties of the eventargs object you're passed. You should always use that.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

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...