msellery Posted March 31, 2003 Posted March 31, 2003 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? Quote
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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] Quote
msellery Posted March 31, 2003 Author Posted March 31, 2003 (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 April 1, 2003 by divil Quote
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
msellery Posted March 31, 2003 Author Posted March 31, 2003 Weird! Would you mind e-mailing me the project you made in full so I can run it next to my code? Please e-mail msellery@hotmail.com. Thanks a lot! Quote
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
msellery Posted March 31, 2003 Author Posted March 31, 2003 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! Quote
msellery Posted March 31, 2003 Author Posted March 31, 2003 UPDATE: You know what works? Using a panel box instead of a picturebox. Panel solved my problem. Thanks again Quote
*Gurus* divil Posted April 1, 2003 *Gurus* Posted April 1, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.