Click here to Skip to main content
15,886,761 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hi,

I have a menu item Clear which should clear the panel from all drawing.
There is no controls existing in this Panel only lines.

I thought it should be easy to clear the Panel from old drawings but it
isn't
I have tried with this code
graphics.Clear(drawingBox1.BackColor);
but the old drawing reback after clearing .

So how can I remove the old drawing and have the Panel as it is when I start
the program
link of my project :

[edit]link/url removed - Please post code instead of url/link[/edit]
Posted
Updated 11-May-11 8:24am
v2

I hope you're not using PictureBox is some other special control to host graphics.

You need to draw immediately in your control (a Panel or a Form, for example). You need to handle the event Paint or override the method OnPaint, use the instance of Graphics supplied in event arguments to draw our your data. When your data is modified, call Control.Invalidate.

Even if you need to save a bitmap, you should not use the bitmap for screen presentation. Instead, abstract out the rendering method like this: void Render(System.Drawing.Graphics graphics) and call this method from Paint or OnPaint. When you need to save your current graphics in the bitmap (file or something), create a bitmap and point on it using your Render method.

Here is how you "clean" or change rendered graphics. You rendering method depends of some data. If the instance of data shows the method that nothing is to be rendered, nothing will be. You only need to change your data and wait for another rendering event WM_PAINT. It will be triggered if you minimize or mask part of the control with other window and show it again.

How do you trigger it programmatically? By calling Control.Invalidate, nothing else. You can also call Invalidate with parameters (Rectangle or Region) to improve performance by requesting only a part if the scene to be invalidated. This is how any kind of animation can be implemented, by the way.

—SA
 
Share this answer
 
Comments
Sander Rossel 11-May-11 14:59pm    
Why is a PictureBox so bad? :)
Sergey Alexandrovich Kryukov 11-May-11 16:27pm    
We discussed it so many times here. It's not bad, it's not designed for the purpose, completely redundant.

This is because is does not provide any value in such cases, just sits between parent control and the user and eats extra resources and performance, and even require extra programming. It's good for some simple cases: put it on container control and use some URL/file/resource to show the ***whole picture***, may be change the picture time to time. As soon as you want to change some detail of the picture just for showing the change on screen, it's a signal to get rid of it.

Control Paint/OnPaint is much easier and more maintainable.
--SA
Sander Rossel 12-May-11 14:30pm    
Actually I am kind of experimenting with this too. I have a Component which should draw some stuff on the hosting Form. Do you suggest I make a _parent.Paint Event Handler in my Class (which could mean I have two Methods handling the same Event)? When moving my mouse over the drawing I also want to show a ToolTip. Sorry for not sticking to the OP's question. But I think in such a case a small PictureBox would be better since you would not always have to check if your mouse is on the location of the drawing. A PictureBox can also more easily change its Location when the hosting Form resizes. To draw in the PictureBox I use its e.Graphics though :)
Sergey Alexandrovich Kryukov 13-May-11 7:13am    
No, it's not easier, PictureBox here just eats extra resource and performance. Easier to move? No easier at all. You draw a fragment of some picture and always calculate some coordinates anyway, right? Make calculations relative to some parameter (such as Origin = {Left, Top}). Make is a parameter (member of class) and move where you need it. Invalidate will put it in place. Not a single benefit of PictureBox, unless this is a heave pixel graphics. But it's better to get rid of it.

You know what? Here is the thing: if this is a sprite with the size withing 32 or 48 pixels or so -- yes PictureBox can be useful. Larger than that -- switch to vector. Another acceptable vector (non-XAML (as XAML is much better)) idea is MetaFile. Consider it as a primitive much like line or ellipse.

--SA
Sander Rossel 13-May-11 13:29pm    
Never thought of it that way. I will try removing my PictureBox and move its code to the draw event. Thanks for answering my question even though this was not my original question. 5 for the answer to the OP, and lots of thanks for the answer to this ;)
I don't know if anyone will download that file, I know for sure I won't...

How are you drawing the lines in the first place?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900