13

I have drawn a circle in windows form

Pen pen = new Pen(Color.Black, 3);
Graphics gr = this.CreateGraphics();
gr.DrawEllipse(pen, 5,5,20,20);

How to delete it...

Ghasem
  • 14,455
  • 21
  • 138
  • 171
Genius
  • 1,084
  • 2
  • 10
  • 20
  • Either use Graphics.Clear() with a desired color, use the logic I posted as an answer, or perform this.Invalidate(). The last will simply force the form to redraw it's client area which will effectively delete the circle. – Chris Hutchinson Nov 08 '10 at 14:23
  • Since you are using `this.CreateGraphics`, you are probably not drawing this in the Paint event. That means any time your form gets redrawn (either because you call the `Invalidate` method or because Windows sends a `WM_PAINT` message), your circle will be erased anyway. If you want the circle to be semi-permanent (at least to stay there until you explicitly erase it), you need to make sure that you draw it in the Paint event handler for your form, using the Graphics object that is passed in the `PaintEventArgs` for the event. – Cody Gray - on strike Nov 08 '10 at 14:37
  • Just minimize the window and restore it again. – Hans Passant Nov 08 '10 at 14:38

9 Answers9

19

You have to clear your Graphic:

Graphics.Clear();

But all drawn figures will be cleared. Simply, you will then need to redraw all figures except that circle.

Also, you can use the Invalidate method:

Control.Invalidate()

It indicates a region to be redrawn inside your Graphics. But if you have intersecting figures you will have to redraw the figures you want visible inside the region except the circle.

This can become messy, you may want to check out how to design a control graph or use any graph layout library.

Community
  • 1
  • 1
Cédric Guillemette
  • 2,394
  • 1
  • 14
  • 22
  • I don't see what a control graph (aka flowchart) has to do with this question. – Ben Voigt Nov 08 '10 at 14:38
  • A control graph is usually at the basis of how a graphic layout engine is built. Therefore, it could help determine what needs to be drawn or invalidated. Its just a opening on the subject... – Cédric Guillemette Nov 08 '10 at 14:51
  • 2
    there is no Graphics.clear, and if you do Graphics gr; gr=....; gr.clear() it gives an error because it needs an argument, a color – barlop Dec 08 '15 at 04:55
  • 1
    @barlop [MSDN](https://msdn.microsoft.com/en-us/library/system.drawing.graphics.clear.aspx) says "Graphics.Clear Clears the entire drawing surface and fills it with the specified background color.". So you just have to do `CreateGraphics().Clear(Form.ActiveForm.BackColor)` – Tyrmos Dec 06 '17 at 12:39
14

You can invalidate the draw region you want to refresh for example:

 this.Invalidate();

on the form...

shin
  • 666
  • 2
  • 9
  • 25
7

Assuming you're subscribing to the Paint event or overriding the protected OnPaint routine, then you will need to perform something like this:

bool paint = false;

protected override void OnPaint(object sender, PaintEventArgs e)
{
  if (paint) 
  {
   // Draw circle.
  }
}

Then when you want to stop painting a circle:

paint = false;
this.Invalidate(); // Forces a redraw
Chris Hutchinson
  • 9,082
  • 3
  • 27
  • 33
  • This has the benefit of allowing the form to redraw it's client area. Using Graphics.Clear will paint over the client area with a solid color. – Chris Hutchinson Nov 08 '10 at 14:19
  • If a square is inside the circle and you invalidate the region surrounding the circle, the square will also be invalidated. Using this technique you will have to draw anything that is inside that region that you may want visible. – Cédric Guillemette Nov 08 '10 at 14:20
  • Which is easily done if you simply put that logic outside the conditional check and force invalidation when you want the form to be redrawn. The OnPaint logic is called every single time the form invalidates a region (or the entire form client area). – Chris Hutchinson Nov 08 '10 at 14:26
4

You can make a figure of same dimensions using the backColor of your control in which you are drawing

use after your code to clear your figure.

Pen p = new Pen(this.BackColor);   
gr.DrawEllipse(p, 5,5,20,20);
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
4

In fact, you can delete your circle and nothing but your circle.

Everything you need is something like a screenshot of the "before state" of the area you want to clear, to make a TextureBrush from it. You can achieve that step by something like this:

Bitmap _Background = new Bitmap(this.Width, this.Height);
Graphics.FromImage(_Background).CopyFromScreen(this.Left, this.Top, 0, 0, this.Size);

The first line will give you a bitmap in your windows forms size. The second line will save a screenshot of it in the _Background-bitmap.

Now you create a TextureBrush out of it:

Brush brsBackground = new TextureBrush(_Background);

The next thing you need are the dimensions of your circle, so you should save them into a variable, if they are not a fix value. When you got them at hand, you can clear the specific area like this:

Graphics gr = this.CreateGraphics();
gr.FillEllipse(brsBackground, 5, 5, 20, 20); // values referred to your example

Done!

Even complex figures are able to be deleted by this, like a GraphicsPath for example:

GraphicsPath gp = new GraphicsPath(); // any kind of GraphicsPath
gr.FillRegion(brsBackground, new Region(gp));
  • this solution worked incredibly well.. i was drawing text over an image, and the text changed dynamically.. using this method you provided, i took a snapshot of the image prior to initial draw. i "cleared" the regions where the text would change, and draw the new test. thanks for you contribution! – Heriberto Lugo Feb 16 '19 at 21:35
3

If u are using Invalidate() and is not working, make a panel.Refresh().

That will work on you.

josep
  • 41
  • 2
  • i think this comments is the solution ..... Invalidate the panel (form,tab,......) is not a good idea .... specially when need to change the graphics after clicks or any frequent change events..... – Rouzbeh Zarandi Jun 30 '17 at 17:36
3

You don't "delete" it per se, there's nothing to delete. It's a drawing, you draw something else over it or you can call the Graphics.Clear() method.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • You're correct, but if he's drawing in Paint/OnPaint then he can simply include a flag to draw a circle or not, then invalidate the form. The form's internal logic will redraw it's client area without the circle. – Chris Hutchinson Nov 08 '10 at 14:18
0

just make another control with the attributes etc. that you want, make the visibility to false and set the region of the control to the other control like this:

pen.Region = pen2.Region;
-7

It is very simple to delete a drawn circle from c.

There is only four steps:-

  1. Open turbo app
  2. go to the command where you had drawn the circle
  3. drag the command
  4. click on delete button
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
jithin
  • 1