Undo/Redo Operations <vb.net>

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
The Undo/Redo is such a common theme in Windows apps that I'm wondering if Windows has a useful class to use that will make things easier on me. I dont know how it'd do it exactly, but asking all the same.

What I want to do, basically, is Undo/Redo changes to a datagridview and some internal variables. The Changes that might occur from a user include row deletions, additions, insertions. But many internal variables are changing with this stuff too.

Is .Net's UndoEngine what i'm looking for? Something else? Or do people usually just code their own undos?
 
The UndoEngine is news to me. In the rare occasion that I added undo/redo to an app, I've rolled my own in the past. The model was similar to the UndoEngine, though.
 
UndoEngine and designers

From the looks of it, the UndoEngine class and those associated with it, are there to support undo/redo transactions within the forms designer, not for general use in applications. But as marble_eater points out, it is a good model to base your own undo/redo system upon.

The idea is that you maintain a stack of operations, or transactions, each of which corresponds with the changes made upon one user interaction. Each transaction needs to store enough information to be able to undo its changes. When an action is performed, a new transaction is created and pushed to the top of the stack. When the user wishes to undo an operation, the top transaction is popped from the stack and used to revert the changes. Overall the concept is not difficult; the tricky part is designing your transaction classes - I suggest creating various subclasses corresponding to different types of actions that might be performed.

You may also wish to implement a redo stack, which undone transactions are added to as they are undone. This will allow the user to redo any actions they undo.

Good luck :cool:
 
A DataSet is the only object provided by .NET that provides native undo, that I'm aware of anyway. If you use it properly (and I know I've not always done it right), you mark rows for Edit and later use AcceptChanges or CancelChanges (that name might not be right). At any time, you can see the original values through various methods - for example, the DataTable Select() method has an argument for DataViewRowState (again, may not be exactly right) to get at OriginalValues, CurrentValues, etc.

Rockford Lhotka's book(s) "Expert C# Business Objects" (he has about 4 versions of this book/idea), describes a custom set of business objects that natively support multiple levels of undo. I own the C# version of the book and have read through a lot of it, but in the end it seemed easier to code our own object model using other pieces we already had built (and understood better), so I can't say that I've really seen how he handles it. If you're looking for references, that book may help. You can also google for CSLA to get more info.

-ner
 
It could get complicated to do an undo/redo function if, as NeuralJack says, internal variables are changing with the dataset too. Is there any scope for saving different versions of the data and variables that he has as files, using serialization say, and just jumping to older versions of the relevant file when the user calls the undo function? Otherwise I feel that the code for the undo/redo is going to be scattered throughout the program, and be quite a hassle to maintain.
 
Back
Top