Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a drawn circle in c# windows form?

Tags:

c#

graphics

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...

like image 250
Genius Avatar asked Nov 08 '10 14:11

Genius


4 Answers

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
like image 91
Chris Hutchinson Avatar answered Nov 06 '22 18:11

Chris Hutchinson


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.

like image 22
Cédric Guillemette Avatar answered Nov 06 '22 17:11

Cédric Guillemette


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

 this.Invalidate();

on the form...

like image 14
shin Avatar answered Nov 06 '22 17:11

shin


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);
like image 4
Javed Akram Avatar answered Nov 06 '22 16:11

Javed Akram