Click here to Skip to main content
14,420,907 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hey
i have created a project that draw an Ellipse via movement of the mouse by the user
and 2 buttons Undo & Redo

The undo works o.k.
but i have a problem with the Redo option

if i draw 3 circles
and then click twice on the undo button i delete 2 circles and left with the first circle
which is great
but when i click the redo button once i want to return the second circle that i draw
but i get the insted the 2 circle (2th and 3th and final)

here is my code

namespace Final_Project___Undo_and_Redo
{
    public partial class Form1 : Form
    {
        private Bitmap PicInMemory; 
        private Graphics PaintOnMyPic;
        private int FirstX, FirstY;
        private Graphics PaintOnPanel;
        List<bitmap> UndoList;
        List<bitmap> RedoList;
        private bool UndoMade = false;
        private bool RedoMade = false;

        public Form1()
        {
            InitializeComponent();
            PicInMemory = new Bitmap(panel1.Width, panel1.Height);
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnMyPic = Graphics.FromImage(PicInMemory);
            PaintOnMyPic.Clear(Color.White);
            UndoList = new List<bitmap>();
            RedoList = new List<bitmap>();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            FirstX = e.X;
            FirstY = e.Y;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Pen MyPen = new Pen(Color.Aqua);
            if (e.Button == MouseButtons.Left)
            {
                    PaintOnPanel = panel1.CreateGraphics();
                    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
                    PaintOnPanel.DrawEllipse(MyPen, FirstX, FirstY, e.X - FirstX, e.Y - FirstY);
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            UndoList.Add(new Bitmap(PicInMemory));
            Pen MyPen = new Pen(Color.Aqua);
            PaintOnMyPic = Graphics.FromImage(PicInMemory);
            PaintOnMyPic.DrawEllipse(MyPen, FirstX, FirstY, e.X - FirstX, e.Y - FirstY);
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            RedoList.Add(new Bitmap(PicInMemory));
        }

        private void BtnUndo_Click(object sender, EventArgs e)
        {
            if (UndoList.Count == 0)
            {
                BtnUndo.Enabled = false;
                return;
            }
            PicInMemory = UndoList[UndoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoList.RemoveAt(UndoList.Count - 1);
            UndoMade = true;
        }

        private void BtnRedo_Click(object sender, EventArgs e)
        {
            if (RedoList.Count == 0 || UndoMade == false)
            {
                BtnRedo.Enabled = false;
                return;
            }
            PicInMemory = RedoList[RedoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoMade = false;
        }
    }
}

Thanks for the help
Posted
Updated 13-May-13 1:56am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

may be this will help:
var selectedItem = RedoList.First();
PicInMemory = selectedItem;
RedoList.RemoveAt(0)
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi, I wish this may help you:
Firstly, your code adds to redo list in MouseUp event so when when you undo twice or more and then redo with the last pic in your redo list. this will redraw the pic before the first undo so I suggest this with deleting UndoMade variable
int undo_times=0;
private void BtnUndo_Click(object sender, EventArgs e)
{
    if (UndoList.Count == 0)
    {
        BtnUndo.Enabled = false;
        return;
    }
    PicInMemory = UndoList[UndoList.Count - 1];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    UndoList.RemoveAt(UndoList.Count - 1);
    undo_times+=1;
}

private void BtnRedo_Click(object sender, EventArgs e)
{
    if (RedoList.Count == 0 || undo_times == 0)
    {
        BtnRedo.Enabled = false;
        return;
    }
    PicInMemory = RedoList[RedoList.Count - undo_times];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    undo_times-=1;
}

with my best wishes.
   

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




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