RichTextBox 中的撤消操作不会撤消粘贴的文本
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据设计,
TextChanged
事件不会触发撤消状态的创建。粘贴会触发TextChanged
事件,因此这就是发生这种情况的原因。为了解决这个问题,我建议将事件处理程序绑定到调用
KeyUp
事件的TextChanged
事件。 KeyUp 事件确实会触发 Undo 状态创建的创建。By design, a
TextChanged
event does not trigger the creation of an Undo state. Pasting triggers theTextChanged
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 theKeyUp
event. The KeyUp event does trigger the creation of Undo state creation.您可以使用剪贴板,而不是显式替换文本。这不会清除撤消堆栈
Instead of replacing text explicitly, you can use the clipboard. And this wouldn't clear undo-stack