RichTextBox 中的撤消操作不会撤消粘贴的文本

发布于 2024-09-11 09:14:25 字数 120 浏览 3 评论 0原文

我有一个 WinForms RichTextBox,默认情况下,撤消功能适用于大多数情况,但是当我粘贴一些文本(我已删除它的格式,它只是纯文本),并尝试撤消它时,它不会撤消刚刚粘贴的文本。

对解决方案有帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2024-09-18 09:14:25

根据设计,TextChanged 事件不会触发撤消状态的创建。粘贴会触发 TextChanged 事件,因此这就是发生这种情况的原因。为了解决这个问题,

我建议将事件处理程序绑定到调用 KeyUp 事件的 TextChanged 事件。 KeyUp 事件确实会触发 Undo 状态创建的创建。

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.

冬天旳寂寞 2024-09-18 09:14:25

您可以使用剪贴板,而不是显式替换文本。这不会清除撤消堆栈

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文