Adding Both Undo and Redo Functionality to the .NET TextBox
If you are working on a project that requires the ability to support undo and redo operations with the standard TextBox control, you are soon to run into the very annoying surprise that Microsoft didn’t add a redo method to the TextBox control to coincide with the Undo Method. You may also find out that the undo method they did implement is not too great, and only supports undoing one change.
As I had to add this functionality, I have created an extension of the TextBox control for everyone to freely use, the source code of which you can find at https://github.com/intninety/temporaltextbox. This post is essentially just giving an overview of it and an example of how to implement it.
For anyone not interested in the summary and implementation explanation that just wants to skip straight to the actual code of the extended class, you can find it here: https://github.com/intninety/temporaltextbox/blob/master/TemporalTextBox/TemporalTextBox.cs
What version of .NET is this compatible with?
As of the time of writing this, it requires 3.5 or later. It is possible to rewrite it to work with 2.0 if you do want to, you’d just have to replace the use of LINQ in the Transform method.
What is better about the undo functionality in TemporalTextBox?
Unlike the standard TextBox, TemporalTextBox will allow you to undo every operation as far back as it being initialised as opposed to only being able to undo the very last operation.
How do I implement it?
The first thing you’ll need to do is grab a copy of the source code from https://github.com/intninety/temporaltextbox.
If you decide to build the DLL from the project then add a reference to it into your own project. Alternatively if you want to include the source code directly in your project, then make sure you add a reference to LINQ.
Once you’ve got the TemporalTextBox reference or class in your project we can get started on implementing it!
To use the control you can either add it via the Toolbox as you normally would in the designer, or you can use the Transform method. The Transform method will consume a standard TextBox and set all the properties of your TemporalTextBox to be the same, allowing it to seemlessly take the place of the one you have already designed your form with.
If you want to try out the transform method then add a standard TextBox control onto your form and tweak it as you want. Now switch to the code view and add a new instance member of type TemporalTextBox. Once in the code view add the following code in the Shown event of your form, replacing temporalTextBox with the name of your TemporalTextBox instance, and textBox1 with the name of your standard TextBox.
|
1 2 3 |
this.temporalTextBox = new TemporalTextBox();
this.temporalTextBox.Transform(this.textBox1, true);
this.Controls.Add(this.temporalTextBox); |
And voila, you now have your TemporalTextBox on your form and are now ready to utilise the undo and redo methods!
Using these doesn’t really require much explanation, simply call the Undo method to undo an operation, and call Redo to redo a previous undo operation; it’s that simple…
If you don’t want to use the improved undo functionality I mentioned previously that is in TemporalTextBox you’ll just have to make sure you set the UseCustomUndoMethod property to false. This will have no effect on the redo functionality you’ll still be able to use the redo method even if using the standard undo method.
How are strings pushed to the undo and redo stacks?
Unless you’re interested in how the undo and redo methods are implemented behind the scenes, you may want to skip this section!
The current text (prior to the key press) is pushed onto the undo stack every time the user presses one of the following keys:
- Backspace
- Delete
- Enter
- Space
In addition to this, when the text is changed it checks to see if the difference between the previous text and the new text is more than a character long, if it is then we push to the stack again (as this would imply a paste operation). If we only have one character then we don’t push to the stack as we don’t want undo operations to be on a per-character basis. If the length of the two strings are equal, then this would imply that we have had a paste operation over an existing piece of text, so we check to see if the strings are also equal, and if they aren’t we push to the stack.
The redo stack is simply pushed to every time we make a call to the undo method.
Got anything to say? Go ahead and leave a comment!
Archives
Recent Posts
- Adding Both Undo and Redo Functionality to the .NET TextBox
- Setting Default CurrentCulture in all Versions of .NET
- Capturing Network Traffic using the WiFi Pineapple, tcpdump and Android
- Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- Cracking PPTP / MS-CHAPv2 with Chapcrack & CloudCracker
Tag Cloud
Recent Comments
- int0x90 on Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- Olivier Jaquemet on Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- ponix on Kid Teaches How to Use Tracert – Fail!
- kat on Final Fantasy XIII: How to Beat Odin
- JD Swagger on Final Fantasy XIII: How to Beat Odin



Posted under: