Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys

i created a paint project with the option of Undo and Redo
After i press on the undo button i have to jump one step higher in the list
in order to be able to show the last action

the problem starts when after first click on the Undo button and then on the Redo button
then when i want to press on the undo button again it's shows me the second cell from the end and not the last

because after every press on the Undo button i Remove the last cell in the list
is there any other way to move back in the list without removing the last cell ?

C#
private void BtnUndo_Click(object sender, EventArgs e)
        {
            PicInMemory = UndoList[UndoList.Count - 1];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            UndoList.RemoveAt(UndoList.Count - 1);         ---> the problem is with this line
            undocounter++;
        }

        private void BtnRedo_Click(object sender, EventArgs e)
        {
            PicInMemory = RedoList[RedoList.Count - undocounter];
            PaintOnPanel = panel1.CreateGraphics();
            PaintOnPanel.DrawImage(PicInMemory, 0, 0);
            undocounter--;
        }


Thank's for the help ...
Posted
Updated 2-Jun-13 21:33pm
v2

Why are you removing an action from your undo list, without adding it to your redo list? And why do you not remove items from the redo list and put them on the undo list?

I suspect that you need to do both of these things, and when you do, your problem may disappear...
 
Share this answer
 
Or use a slightly different approach:

Keep just one list of operations plus the current index of the last operation.
On Undo: decrement index.
On Redo: increment index.
On Operation: Throw away what's after index in the list, append operation, increment index.
 
Share this answer
 
C#
private void BtnUndo_Click(object sender, EventArgs e)
{
    Counter++; 
    PicInMemory = UndoList[UndoList.Count - Counter]; 
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    UndoMade = true;
    BtnRedo.Enabled = true;
}

private void BtnRedo_Click(object sender, EventArgs e)
{
    PicInMemory = RedoList[RedoList.Count - Counter];
    PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    Counter--;
}
 
Share this answer
 
v2
Comments
lukeer 4-Jun-13 3:06am    
Please wrap code in tags: <pre lang="c#">YourCodeHere();</pre>
It gets much more readable this way.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900