I've been looking for a while now and haven't found an easy or correct way to solve the following issue.
I have a multi-line TextBox with the
ShortcutsEnabled = true
which, of course, allows the user to easily undo or redo any changes made by using the keyboard shortcuts (e.g. Ctrl
Now I wish to implement a button that clears the text of the TextBox but still allow the user to undo the action by using the default undo function.
Clearing the text from the TextBox is easy:
myTextBox.Text = string.Empty;
The problem is that the default undo function does not work when replacing the text these ways.
However, if the user selects all the text ( Ctrl
) and hits Delete
manually, the TextBox becomes empty and the user can still restore the deleted data by Ctrl
I know that writing my own implementation of
would probably allow me to get the desired result but there has to be another way.
I simply need to be ably to mimic the manual select all and delete method in code.
And this is where I need some help, how can I implement this?
TextBox doesn't seem to have a default Delete
text function that I can call.
As a proof of concept I've tried implementing this using the
method (or Ctrl
) , which actually does what I want. The only downside is that this replaces potentially important clipboard data. I've tried coming up with a workaround for this as well, but after doing some research there doesn't seem to be any "correct" or at least "great" way to restore clipboard data.
var cbData = Clipboard.GetText();
How can I delete the text from a TextBox in code the same way a user would do manually? So that the default
method can still be called to restore the deleted text?