Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Undo rectangle
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)
            {
                // Must be within constraint, prevents tiny invisible rectangles from being added
                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))
            {
                // Redraws all existing rectangles onto the form
                foreach (Rectangle rect in rects)
                    e.Graphics.DrawRectangle(pen, rect);
 
                // Must be within constraint, prevents tiny invisible rectangles from being added
                if (tempRect.Width >= 10 && tempRect.Height >= 10)
                    e.Graphics.DrawRectangle(pen, tempRect);
 
            }
            using (Pen pen1 = new Pen(Color.Black, 2))
            {
                // Redraws all existing rectangles onto the form
                foreach (Rectangle rectOval in oval)
                    e.Graphics.DrawEllipse(pen1, rectOval);
 
                // Must be within constraint, prevents tiny invisible rectangles from being added
                if (tempOval.Width >= 10 && tempOval.Height >= 10)
                    e.Graphics.DrawEllipse(pen1, tempOval);
            }
 

        }
 

 

 
    }
}
[Edit]Code block added[/Edit]
Posted 2-Mar-13 6:49am
Edited 2-Mar-13 6:50am
ProgramFOX127.9K
v2
Comments
ThePhantomUpvoter at 2-Mar-13 11:55am
   
Since you want someone to do this for you, have you considered going to a rent-a-coder site and hiring someone? What don't you just create an OnPaint handler and draw whatever you want (or not) in that?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Since you add your objects to a list (or two) here is how I would do it.
1) Create an abstract MyShape class, and derive a MyRectangle and MyOval from it. Probably, each should provide code to draw themselves, given an appropriate Graphics object
2) Replace my two lists with a single MyShape list.
3) Add a ReDo MyShape list.
4) When you create a rectangle or oval with the mouse, add it as an appropriate MyShape object to the MyShapes list, and clear the ReDo list. Then Invalidate as you are.
5) Change the Paint handler to draw from the single MyShapes list.
7) To Undo, remove an item from the end of the MyShapes list, and add it to the Redo list. Then Invalidate.
8) To Redo, remove an item from the Redo list, and put it back on the MyShapes list.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 2 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100