this is my below code which draw rectangle and eclipse on mouse event..i try search undo alots.but fail to undo mycode..i want dat wen i click on button then last rectangle remove from drawing.pls dnt give me link.modify my code coz am not very much experience on graphics.simply click button undo perform,last draw rectangle or eclipse will remove..clicking button rrmoving previous rec or eclipse
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace ControlShapePaint
{
public partial class DrawShapes : UserControl
{
private readonly List<Rectangle> rects = new List<Rectangle>();
private readonly List<Rectangle> oval = new List<Rectangle>();
public bool isOval;
public bool check = false;
private Point tempStartPoint;
private Point tempStartPointoval;
private Rectangle tempRect;
private Rectangle tempOval;
public DrawShapes()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void toolStripButtonRectangle_Click(object sender, EventArgs e)
{
isOval = true;
check = true;
}
private void toolStripButtonEllipse_Click(object sender, EventArgs e)
{
isOval = false;
check = true;
}
private void DrawShapes_MouseDown(object sender, MouseEventArgs e)
{
if (check == true)
{
if (isOval == true)
{
tempStartPoint = e.Location;
}
else if (isOval == false)
{
tempStartPointoval = e.Location;
}
Invalidate();
}
}
private void DrawShapes_MouseMove(object sender, MouseEventArgs e)
{
if (check == true)
{
if (e.Button != MouseButtons.Left)
return;
if (isOval == true)
{
Point tempEndPoint = e.Location;
tempRect =
new Rectangle(
Math.Min(tempStartPoint.X, tempEndPoint.X),
Math.Min(tempStartPoint.Y, tempEndPoint.Y),
Math.Abs(tempStartPoint.X - tempEndPoint.X),
Math.Abs(tempStartPoint.Y - tempEndPoint.Y));
}
else if (isOval == false)
{
Point tempEndPoint1 = e.Location;
tempOval =
new Rectangle(
Math.Min(tempStartPointoval.X, tempEndPoint1.X),
Math.Min(tempStartPointoval.Y, tempEndPoint1.Y),
Math.Abs(tempStartPointoval.X - tempEndPoint1.X),
Math.Abs(tempStartPointoval.Y - tempEndPoint1.Y));
}
Invalidate();
}
}
private void DrawShapes_MouseUp(object sender, MouseEventArgs e)
{
if (check == true)
{
if (isOval == true)
{
if (tempRect.Width >= 10 && tempRect.Height >= 10)
rects.Add(tempRect);
}
else if (isOval == false)
{
if (tempOval.Width >= 10 && tempOval.Height >= 10)
oval.Add(tempOval);
}
}
}
private void DrawShapes_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 2))
{
foreach (Rectangle rect in rects)
e.Graphics.DrawRectangle(pen, rect);
if (tempRect.Width >= 10 && tempRect.Height >= 10)
e.Graphics.DrawRectangle(pen, tempRect);
}
using (Pen pen1 = new Pen(Color.Black, 2))
{
foreach (Rectangle rectOval in oval)
e.Graphics.DrawEllipse(pen1, rectOval);
if (tempOval.Width >= 10 && tempOval.Height >= 10)
e.Graphics.DrawEllipse(pen1, tempOval);
}
}
}
}
[Edit]Code block added[/Edit]