Reputation: 1343
I need to be able to detect if an "undo" has been triggered, and whether or not it has had an effect on the contents of my RichTextBox
.
Of I type content into the RichTextBox
at the minute, and press Ctrl+Z, windows seems to handle the undo for me. I want to be able to write code that will get triggered straight after that. I have been looking around and can't find anything.
Thanks in advance.
Upvotes: 2
Views: 1412
Reputation: 2813
As already described, CommandBindings
can be used. I prefer binding to each control
instead of binding to all controls of a specific class. This can be done in the following way:
this.richTextBox.CommandBindings.Add(
new CommandBinding(ApplicationCommands.Undo, this.RichTextBoxUndoEvent));
private void RichTextBoxUndoEvent(object sender, ExecutedRoutedEventArgs e)
{
e.Handled = true;
this.richTextBox.Undo();
}
Upvotes: 0
Reputation: 1401
Starting from .Net 3.0, there is a simple built-in way to get notified when an undo command (among others) is executed:
CommandManager.RegisterClassCommandBinding(typeof(MyClass),
new CommandBinding(ApplicationCommands.Undo, OnUndo));
Just call this line of code in the static constructor (or somewhere else) and add a static method:
private static void OnUndo(object sender, ExecutedRoutedEventArgs e)
{
//your code
}
Upvotes: 2
Reputation: 698
If I well understand you, you want to compare content before and after the Ctr+Z
.
Then you should do :
In XAML File :
<RichTextBox PreviewKeyDown="RichTextBox_PreviewKeyDown" KeyUp="RichTextBox_KeyUp" />
In CS File :
private void RichTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z)
{
Console.WriteLine("After : " + new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text);
}
}
private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z)
{
Console.WriteLine("Before : " + new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text);
}
}
Then, you will see in the output of your application the content of your RichTextBox before the Ctrl+Z
and the content after.
I've try it and it works fine !
Upvotes: 0
Reputation: 16623
WINFORM:
You could exploit the KeyDown
event and detect if Ctrl+Z is pressed:
richTextBox.KeyDown += new KeyEventHandler(richTextBox_KeyDown);
private void richTextBox_KeyDown(object sender, KeyEventArgs e){
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z){
//undo detected, do something
}
}
WPF :
richTextBox.KeyUp += new KeyEventHandler(richTextBox_KeyUp);
void richTextBox_KeyUp(object sender, KeyEventArgs e) {
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z) {
//undo detected, do something
}
}
Upvotes: 1
Reputation: 21306
I think you're going to have to implement that yourself. I'm not aware of an event out of the box that will suit your needs.
You might also want to have a look at Monitored Undo Framework.
And here for additional reading.
Upvotes: 0