Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 11-May-11 8:04am
Edited 11-May-11 8:24am
Kim Togo25.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Naerling at 11-May-11 14:59pm
   
Why is a PictureBox so bad? :)
SAKryukov at 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
Naerling at 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 :)
SAKryukov at 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
Naerling at 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 ;)
Kim Togo at 12-May-11 5:11am
   
Good answer SA. My 5.
SAKryukov at 13-May-11 7:06am
   
Thank you, Kim.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  

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

  Print Answers RSS
0 CPallini 345
1 BillWoodruff 324
2 George Jonsson 279
3 Sergey Alexandrovich Kryukov 258
4 OriginalGriff 227
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 11 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100