How to realize Undo/Redo
hi c# community!
have anybody of you ever realized something like Undo/Redo in C# ?
i need some informations, ...links, ... examples... ? or how could i start...
thanks a lot
jerry
hi c# community!
have anybody of you ever realized something like Undo/Redo in C# ?
i need some informations, ...links, ... examples... ? or how could i start...
thanks a lot
jerry
The TextBox control and the RichTextBox control support Undo and Redo methods natively you all you need to do is call the appropriate methods if your application uses these controls.
I'm working on a framework that supports deep undo/redo capability. It's not a task for the squeamish! From my research, the general consensus is that the Command pattern is the best for Undo / Redo. Here’s a basic “UndoBuffer”. It should get you started…
public interface IUndoCommand
{
/**
* Re-executes a command.
*/
void Redo();
/**
* Reverses the effect of executing the command.
*/
void Undo();
}
public class BasicUndoBuffer
{
public void AddCommand(IUndoCommand command)
{
redoStack.Clear();
undoStack.Add();
}
public void UndoCommand()
{
IUndoCommand cmd = undoStack.Pop();
try
{
cmd.Undo();
}
finally
{
redoStack.Push(cmd);
}
}
public void RedoCommand()
{
IUndoCommand cmd = redoStack.Pop();
try
{
cmd.Redo();
}
finally
{
undoStack.Push(cmd);
}
}
private Stack<IUndoCommand> undoStack = new Stack<IUndoCommand>();
private Stack<IUndoCommand> redoStack = new Stack<IUndoCommand>();
}
NOTE: Every place I found, defined an ICommand with THREE methods, Do, Undo, and Redo. I chose to use just Undo and Redo, feel free to do otherwise.