views:

88

answers:

2

I have a WinForms RichTextBox and by default the Undo works for most things, but when I Paste some Text in (I have stripped it of formatting it is just plain text), and try undo it does not undo the text just pasted.

Any help to a solution?

+2  A: 

By design, a TextChanged event does not trigger the creation of an Undo state. Pasting triggers the TextChanged event, so that's why this is happening. To get around it,

I would recommend binding an event handler to the TextChanged event that invokes the KeyUp event. The KeyUp event does trigger the creation of Undo state creation.

SimpleCoder
How to do this? In C#
Viion
A: 

Instead of replacing text explicitly, you can use the clipboard. And this wouldn't clear undo-stack

  var originalClbData = Clipboard.GetDataObject(); 
  Clipboard.SetText(newText);
  txtMailBody.SelectAll();
  txtMailBody.Paste();
  if (originalClbData != null) Clipboard.SetDataObject(originalClbData);
Ike