Hi, i just want to ask a quick question.
I wish to draw a shape in C# using the GDI+ and .net framework 3.5 in visual studio 2008, next i would like to delete the shape i have drawn when i press a button, however i do not wish to remove everything which is on the screen as i have some static GDI+ shapes in which i wish to keep on the screen.
So say that i draw a circle at a point on the screen where the mouse is clicked, how can i remove this when i click on a button? Also say i draw three circles when i click the mouse at three given points on the screen, how can i remove all of these when i click on a button?
I can draw the shapes via mouse click and probably store the co-ordinates in an arraylist or something myself but what i do not know how to do is remove these shapes when they are no longer needed.
Thanks
Loading

Danatas GerviRank #4,152 · 361 reputationPosted Aug 4, 2009, 4:41 AM EDT
private void button1_Click(object sender, EventArgs e) { m_Circles.Clear(); this.Refresh(); } private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(m_RedPen, 20, 20, 40, 40); foreach (CoordinateKeeper oneCircle in m_Circles) { e.Graphics.DrawEllipse(m_chirclePen, oneCircle.CircleRegion); } }The is the real reason to optimise the Paint event handler. This handler may be called many times in second - for example, when user will drag other window over the form.
Graphics object alredy exist in
PaintEventArgs- so, we should use it.
Danatas GerviRank #4,152 · 361 reputationPosted Aug 3, 2009, 8:35 AM EDT
This is not so easy, as we want. :-)
Even if you want to keep the graphic on form after resizing – you should redraw it on form Paint event. To do so, your form should keep collection of graphics objects("Coordinates Keeper" ) – with all necessary data to repaint it.
If you will create such collection, you can easy erase the graphics from screen by removing the instance of "Coordinates Keeper" from collection, and calling Refresh of form.
Mark33 reputationPosted Aug 3, 2009, 8:51 AM EDT
Thanks
Danatas GerviRank #4,152 · 361 reputationPosted Aug 3, 2009, 10:24 PM EDT
namespace PaintDeleteCircles { public partial class Form1 : Form { Pen m_chirclePen; List m_Circles; public Form1() { InitializeComponent(); m_chirclePen = new Pen(Brushes.Chocolate, 3.5F); m_Circles = new List(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { this.CreateGraphics().DrawEllipse(m_chirclePen, e.X - 10, e.Y - 10, 20, 20); m_Circles.Add(new CoordinateKeeper() { CircleRegion = new Rectangle(e.X - 10, e.Y - 10, 20, 20) }); } private void button1_Click(object sender, EventArgs e) { m_Circles.RemoveAt(m_Circles.Count - 1); this.Refresh(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics formGraf = this.CreateGraphics(); foreach (CoordinateKeeper oneCircle in m_Circles) { formGraf.DrawEllipse(m_chirclePen, oneCircle.CircleRegion); } } } class CoordinateKeeper { public Rectangle CircleRegion; } }I added on Form1 - only one button - "Delete".
Mark33 reputationPosted Aug 4, 2009, 3:35 AM EDT
Basically everytime i was trying to draw a rectangle with your code it kept on removing that too with the circles, i think it was because i was using the same graphics object for it, so i created a seperate graphics object and use that. Now the rectangle remains and the circles disappear when i click on the button. This is the effect i wanted as i wanted other graphics objects to remain and just have the circles removed.
Here is my new code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace removegdishapetest { public partial class Form1 : Form { Pen m_chirclePen; List m_Circles; public Form1() { InitializeComponent(); m_chirclePen = new Pen(Brushes.Chocolate, 3.5F); m_Circles = new List(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { this.CreateGraphics().DrawEllipse(m_chirclePen, e.X - 10, e.Y - 10, 20, 20); m_Circles.Add(new CoordinateKeeper() { CircleRegion = new Rectangle(e.X - 10, e.Y - 10, 20, 20) }); } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < m_Circles.Count;) { m_Circles.RemoveAt(i); } //m_Circles.RemoveAt(m_Circles.Count - 1); this.Refresh(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 20, 20, 40, 40); Graphics formGraf = this.CreateGraphics(); foreach (CoordinateKeeper oneCircle in m_Circles) { formGraf.DrawEllipse(m_chirclePen, oneCircle.CircleRegion); } } } class CoordinateKeeper { public Rectangle CircleRegion; } }Many thanks for the help, if you think i've "over coded" something then you may modify it again. Other than that it is a thumbs up.
Nice one
Mark33 reputationPosted Aug 4, 2009, 4:57 AM EDT
Ok so i have modified that code and did the rectangle code in which you supplied in the last bit of code. The rectangle still remains and the circles can either be removed all at once or one at a time by using count -1 etc.
Thanks for all of the help man, very much apprechiated. I still cannot believe i missed that .clear() and used a loop instead, having me in stitches laughing so much about it.
Thanks for the help, problem now solved