I have a TextBox which I would like to implement undo/redo functionality for. I have read that it might have some slight undo functionality already, but that it is buggy? Anyways, I would like to implement both undo and redo functionality also just to learn how you would go ahead and do that.
I have read about the Memento Pattern and looked some on a Generic Undo/Redo example on CodeProject. And the pattern kiiind of makes sense. I just can't seem to wrap my head around how to implement it. And how to do it effeciently for the contents of a TextBox
.
Of course I could just store textbox.Text
when TextChanges
, but that would hug up quite a lot of memory pretty fast, especially if the TextBox
contained a lot of text.
So anyways, I'm looking for some advice on how to implement a good, clear and efficient way of implementing this functionality. Both in general and especially for a TextBox c",)
Which data structure is used in redo-undo feature? Explanation: Stack data structure is most suitable to implement redo-undo feature. This is because the stack is implemented with LIFO(last in first out) order which is equivalent to redo-undo feature i.e. the last re-do is undo first.
To implement Undo , you work backward from the tail of the linked-list, using a 'current-node' pointer or index: where the change was insert , you do a delete but without updating the linked-list; and where it was a delete you insert the data from the data in the linked-list buffer.
Undo is an interaction technique which is implemented in many computer programs. It erases the last change done to the document, reverting it to an older state. In some more advanced programs, such as graphic processing, undo will negate the last command done to the file being edited.
The .NET System.ComponentModel
namespace comes with an IEditableObject
interface, you could also use INotifyPropertyChanging
and INotifyPropertyChanged
. MVC Pattern would also make it that your interface responds to changes in the model through events thus updating or restoring the value of your textbox.
Effectively the Memento Pattern.
Have you had a look into these? Here is a how to.
A simple and quicker version would be to store the state of the textbox OnTextChanged
. Each undo would return the last event in an Array. The C# Stack Type would be handy here. You could clear the state once you are off the interface also or after Apply
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With