C#: Unable to undo inserted text
Asked Answered
E

3

6

I am programatically adding text in a custom RichTextBox using a KeyPress event:

SelectedText = e.KeyChar.ToString(); 

The problem is that inserting text in such a way doesn't trigger the CanUndo flag.

As such, when I try to Undo / Redo text (by calling the Undo() and Redo() methods of the textbox), nothing happens.

I tried programatically evoking the KeyUp() event from within a TextChanged() event, but that still didn't flag CanUndo to true.

How can I undo text that I insert without having to create lists for Undo and Redo operations ?

Thanks

Elane answered 24/3, 2011 at 15:44 Comment(0)
E
3

I finally decided to create my own undo-redo system using stacks.

Here's a quick overview of how I did it :

private const int InitialStackSize = 500;    
private Stack<String> undoStack = new Stack<String>(InitialStackSize);
private Stack<String> redoStack = new Stack<String>(InitialStackSize); 

private void YourKeyPressEventHandler(...)
{
        // The user pressed on CTRL - Z, execute an "Undo"
        if (e.KeyChar == 26)
        {
            // Save the cursor's position
            int selectionStartBackup = SelectionStart;

            redoStack.Push(Text);
            Text = undoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;
        }
        // The user pressed on CTRL - Y, execute a "Redo"
        if (e.KeyChar == 25)
        {
            if (redoStack.Count <= 0)
                return;

            // Save the cursor's position
            int selectionStartBackup = SelectionStart + redoStack.ElementAt(redoStack.Count - 1).Length;

            undoStack.Push(Text);
            Text = redoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;

            return;
        }    

        undoStack.Push(Text);
        SelectedText = e.KeyChar.ToString();  
}
Elane answered 24/3, 2011 at 18:37 Comment(0)
V
1

It's just an idea but what if you set the caret position to where you would insert your text and instead of modifying the Text property, just send the keys?

SendKeys.Send("The keys I want to send");

There are bound to be quirks but as I said, it's just an idea.

Viburnum answered 24/3, 2011 at 15:57 Comment(1)
Thanks, but that still didn't trigger the CanUndo flag for some reason.Elane
F
0

You can use TextBox.Paste. The documentation in the class overview, saying "Sets the selected text to the specified text without clearing the undo buffer.", seems confusing. I have just tried it and it sets the Undo as expected.

Is spite of its name it has no relation to Clipboard at all, it just replaces the currently selected text with the text you provide as an argument, and therefore seems just to do what the question asks for, in very simple manner.

Farrel answered 28/7, 2015 at 13:37 Comment(1)
Paste(string) only pastes from clipboard. The string is not what you want to paste but the type of what to paste.Zurkow

© 2022 - 2024 — McMap. All rights reserved.