Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I am new on this so i need some help.
I have just finished my first Windows forms application and it's time to insert the undo/redo code.
The application uses a two-dimensional array on which every data from comboboxes, textboxes, checkboxes, labels etc are stored. I need code to undo/redo the changes of this two-dimensional array. Can anyone help?

What I have tried:

I have been searching for this, but up to now i have just find how to achieve undo/redo on a two-dimensional datagridview. Unfortunately, the array i am using is quite big and the use of a datagridview instead, makes the app really slow.
Posted
Updated 14-Jan-17 12:22pm
Comments
[no name] 14-Jan-17 16:01pm    
"Can anyone help?", I am sure that some could help but you didn't say what specifically you needed help with.

You can create a dynamic array that will store all the actions that are performed.
for example:
VB
'Create a arraylist to store states
dim actions as new arraylist
'call CreateAction to register a state
Private sub CreateAction()
   'Clone (create a copy) your data and store in an object, say data
   actions.add(data)
End Sub

To undo, you simply go back and retrieve the data from the array. 
k = 1 is one step back
k = 2 is two steps back
Private sub UndoAction(byval k as integer)
   data = actions(actions.count - 1 - k)  
End Sub 


A better way is to register both state and action so that the action can be redone.
I don't know how that can be done.
the method above worked for me.
 
Share this answer
 
There are tons of undo/redo code examples here on CodeProject. Or, you can also look at some old code I wrote (RandomMonkeyWorks - Undo/Redo in C++[^]). It isn't VB.Net, and you would have to modify it syntactically to your language, but that should provide a framework for applying in your case.

If you use my framework (and several other frameworks), you would have to add processing in the command handlers to populate the past actions stack, and write undo and redo code to handle the actual undoing and redoing of processing of the 2d array. I would simply create a list of previous values, and the x,y positions of those values, in the past actions stack. Then, after you have tied 'Ctrl-Z' and 'Ctrl-Y' (or whatever command signal) into 'undo' and redo, you use the 'undo' and 'redo' code to find the current value of that x,y position, store it, and place the next (or previous) value into that x,y position.

A simple example is given on the page I linked to.

Hope this helps.
 
Share this answer
 

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