quick pictureBox question

msellery

Member
Joined
Mar 19, 2003
Messages
17
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?
 
Do you mean youre 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, dont use Invalidate inside the Paint() event, because that will cause it to CALL the Paint event; this can cause stack overflow, I think.[/edit]
 
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.

Code:
    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
 
Last edited by a moderator:
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.
 
All I did was create a new project, put a PictureBox on it and paste
in your code. Nothing more. I dont see the point in sending out the
project.
 
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. Ill keep messing with it I guess. Thanks for the suggestions though!
 
UPDATE: You know what works? Using a panel box instead of a picturebox. Panel solved my problem. Thanks again
 
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 youre passed. You should always use that.
 
Back
Top