Hi,
I am trying to implement Undo and Redo Operations in C#.NET. Can
anyone of you give me an idea how I can perform this
Regards,
spshah
Hi,
I am trying to implement Undo and Redo Operations in C#.NET. Can
anyone of you give me an idea how I can perform this
Regards,
spshah
Implementing undo and redo operations in C#
turczytj
Hi Mohammad,
Thank you very much for the links that you have provided.But i wanted to know whether i could undo and redo the objects. Like in my application i m using the inkcanvas and i m placing the rectangle and ellipse on it.Now i wanted to provide the undo and redo functionality on this. That is if i m placing the rectangle on the inkcanvas and then pressing the undo button, then the rectangle should be undone.Can you please help how could i do this.
micronax
You can use Command Pattern to achive Undo/Redo
For more Info look at
http://www.dofactory.com/Patterns/PatternCommand.aspx
and also this article
http://www.codeproject.com/csharp/undoredobuffer.asp
budbjames
Try something similiar like:
public class Rectangle
{
private Stack _stack;
private int height = 10;
private int width = 10;
public void BeginEdit()
{
if (_stack == null)
{
_stack = new Stack();
}
Hashtable state = new Hashtable(4);
state["height"] = height;
state["width"] = height;
_stack.Push(state);
}
public void CommitEdit()
{
Hashtable editState = (Hashtable) _stack.Pop();
editState.Clear();
editState = null;
if (_stateStack.Count == 0)
{
_stateStack = null;
}
}
public void RollbackEdit()
{
Type type = this.GetType();
Hashtable editState = (Hashtable)_stateStack.Pop();
this.height = (int) editState["height"];
this.width = (int) editState["width"];
editState.Clear();
editState = null;
if (_stateStack.Count == 0)
{
_stateStack = null;
}
}
}
Then when a user begins to edit or create a rectangle on the screen, u create the object and run BeginEdit() everytime a user makes a change. The state of the object will be saved and pushed on a stack. When the user undo's an action, u can simply call Rollback() and redraw the rectangle.