Search code examples
c#winformsstackundoredo

Can't get my head around implementing an Undo/Redo functionality, should I use a Stack?


I'm getting kind of confused right now, having one of those days I guess.

I need to implement an Undo and Redo functionality for a form. For simplicities sake, let's say that I only save the control which was modified and the value it had when it left Focus.

How do I save this information in a way that lets me go back or forth in the 'timeline'.

I thought about using a Stack, but while I was testing my little demo, I had a mild aneurysm and here I am.

Code needed, not really but would help. I'm more interested in the algorithm I'd need to implement. Any suggestions?


Solution

  • Yes, you would use a stack. There are a couple ways to do it; read these references:

    http://en.wikipedia.org/wiki/Command_pattern

    http://en.wikipedia.org/wiki/Memento_pattern

    Each has its pros/cons.