alright dudes/dudettes, i have a question about removing shapes in C#.
firstly i am wanting to draw a small circle on the screen at the co-ordinates where the mouse is clicked, then i wish to remove this circle when a button is pressed. Now i could create as many circle as i like and then click on the button and they would all need to be removed.
For this i could use the GDI+ or picture boxes but i am unsure which would be the best way to go. Picture boxes are fine but i don't want to be having picture boxes all over the place. However when when using GDI+ i am unsure how to delete a shape i have previously drawn.
So what is the best way in C# using .net framework to draw and remove shapes on the screen? I am just after using the GDI+ and any form controls in .net framework 3.5, no dll extensions.
p.s. i do not wish to remove all GDI+ graphics when the button is clicked, just the circles in which have been drawn. I know how to draw circles on the screen using GDI+ and how to use form controls but i do not know how to remove a shape drawn with the GDI+.
Last edited by Sharkadder; August 3rd, 2009 at 10:13 AM.
You should be doing all of your drawing in OnPaint (GDI+ and using a PictureBox are not really separate options, GDI+ is simply the API for drawing to the screen). Keep each shape in some type of data structure (Rectangles probably) and draw them to the supplied Graphics object in OnPaint. When the user presses the button, simply empty out your collection of shapes and call invalidate.
ah i see, well i have used onpaint before and i have managed to change the colour of shapes before but not draw them depending on a mouses co-ordinate.
I guess the part i am really confused about is removing the shape. You reckon that i should draw rectangles depending on the mouses x and y position, give it a name and when i want to remove the shapes, remove that shape name and all of the shapes will be removed when i invalidate. Is that what you mean?
I think you need to split these things apart a bit. For example, override OnMouseDown/OnMouseMove to grab the coordinates. Store the mouse state (up or down) and the coordinates in methods. That's all, just store information. If the mouse state has changed, or if you are moving with the mouse down, call Invalidate( ). Now, the OnPaint method simply draws according to the information already stored by the mouse handlers. There is no reason to get mouse coordinates from within OnPaint, OnPaint need only know about how many shapes to draw at which locations.