Click here to Skip to main content
16,016,227 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a problem in my project.this is a part of my code:
C#
private void draw_notes(int x, int y)
        {
            y = (y / 5) * 5;
            
            note.DrawString("w", f1, Brushes.Black, x - 20, y - 41);
            for (int i = 9; i <= 15; i = i + 2)
            {

whit this code I can draw "w" but I can not erase some of them .
I tested
C#
note.Clear(Color.color);

but with this code whole panel will erased.

What I have tried:

I used ---note.Clear(Color.color)--- but it dosen't work because I want to clear some of them not all
Posted
Updated 1-Jul-16 0:08am

1 solution

Basically speaking, you can't, not in the way you mean. It's possible to "erase" a drawn component on a panel, by drawing over it with the background, but that doesn't actually erase it - it doesn't "restore" any text or image parts that it overlapped, so they end up with big gaps in them.

The only way to do this properly is to change the way you draw things (which would be a good idea anyway - you aren't supposed to "hang on" to Graphics objects even if you created them).
Instead of drawing things when you want to, draw them all in the Panel.Paint event handler from a list of "objects" to draw, and call Invalidate on the panel when you change the list. That way, you can remove an object from the list and it will be erased from the panel without affecting other objects.
The Paint event supplies the Graphics object as part of the PaintEventArgs so you don't need to create your own.

It also has the advantage that the drawing is "persistant" - it will be redrawn as it was when you app is minimized and restored for example. Your current method does not allow for that.
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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