views:

3910

answers:

4

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",)

+1  A: 

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:

http://msdn.microsoft.com/en-us/library/system.componentmodel.ieditableobject.aspx

Hope this helps:

A simple and quicker version would be to store the state of the textbox on Text Changed. 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.

Andrew

REA_ANDREW
so, would you make an extended version of a textbox then? implementing that interface?
Svish
How many levels of undo would you require n levels or 1 ? Also would the undo behaviour be on an object or solely on the data of the textbox?
REA_ANDREW
A: 

Here's a way to achieve it with minimal code: (This is the code behind of a win form with a single textbox on it)

public partial class Form1 : Form
{
    Stack<Func<object>> undoStack = new Stack<Func<object>>(); 
    public Form1()
    {
        InitializeComponent();
    }
    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.U && Control.ModifierKeys == Keys.Control && undoStack.Count > 0)
            undoStack.Pop()();            
    }
    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if (e.KeyChar != 'u' || Control.ModifierKeys != Keys.Control)
        {
            var textBox = (TextBox)sender;
            undoStack.Push(textBox.Text(textBox.Text));
        }
    }
}
public static class Extensions
{
    public static Func<TextBox> Text(this TextBox textBox, string text)
    {            
        return () => { textBox.Text = text; return textBox; };
    }
}

By implementing an extension method for other input types the undoStack can service the whole of your UI, undoing all UI actions in order.

jdoig
A: 

I would listen for a change event, and when it occurs push the diff of the previous state and present state onto a stack. The diffs should be much smaller than storing the entire text. Also, you might not want to push a new undo state onto the stack at every edit... I'd lump all typing together until the user changes the cursor position, for example.

Mark
A: 

I use UndoEngine at www.undomadeeasy.com. Funnily enough, its worked example is how to do a character by character undo on a textbox. Hope that helps.

Carl Warman