如何清除TextBox,以使默认的Undo方法仍然起作用? [英] How to clear a TextBox so that the default Undo method still functions?

查看:47
本文介绍了如何清除TextBox,以使默认的Undo方法仍然起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了一段时间,还没有找到简单或正确的方法来解决以下问题.

I've been looking for a while now and haven't found an easy or correct way to solve the following issue.

我有一个带有 ShortcutsEnabled = true 的多行文本框,它当然允许用户轻松撤消或重做使用键盘快捷键(例如 Ctrl + Z ).
现在,我希望实现一个按钮,该按钮可清除TextBox的文本,但仍允许用户使用默认的undo功能来撤消操作.

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 + Z).
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.Clear();  
//or 
myTextBox.Text = string.Empty;

问题在于,以这种方式替换文本时,默认的撤消功能不起作用.
但是,如果用户选择所有文本( Ctrl + A )并手动单击 Delete ,则TextBox将变为空并且用户仍可以恢复通过 Ctrl + Z 删除数据.

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 + A ) and hits Delete manually, the TextBox becomes empty and the user can still restore the deleted data by Ctrl + Z.

我知道编写自己的 Undo Redo 实现可以使我获得所需的结果,但是必须有另一种方法.
我只需要能熟练地模仿手动全选并删除代码中的方法即可.

I know that writing my own implementation of Undo or Redo 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似乎没有我可以调用的默认删除删除替换文本函数.
作为概念验证,我尝试使用 Cut 方法(或 Ctrl + X )实现此目的,实际上可以实现我想要的功能.唯一的缺点是,这将替换潜在的重要剪贴板数据.我也尝试过解决此问题的方法,但是在进行了一些研究之后,似乎没有任何正确"或至少伟大"的方式来还原剪贴板数据.

TextBox doesn't seem to have a default Delete, Remove or Replace text function that I can call.
As a proof of concept I've tried implementing this using the Cut method (or Ctrl + X ) , 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();
myTextBox.SelectAll();
myTextBox.Cut();
Clipboard.Clear();
if(!string.IsNullOrEmpty(cbData))
     Clipboard.SetText(cbData);


TLDR;

如何以与用户手动操作相同的方式从文本框中删除文本?这样,仍可以调用默认的 Undo 方法来恢复已删除的文本吗?

How can I delete the text from a TextBox in code the same way a user would do manually? So that the default Undo method can still be called to restore the deleted text?

推荐答案

如何聚焦文本框,选择所有文本,然后模拟退格键?与用户手动清除TextBox相同.(至少,我以这种方式清除了文本字段)

What about focusing the TextBox, selecting all text and then simulating a backspace? It's the same as the user manually clearing the TextBox. (Atleast, I clear text fields in this way)

textBox1.Focus();
textBox1.SelectAll();
SendKeys.Send("{BACKSPACE}");


部分相关的事实-在WPF中,无论您如何更改文本, Undo Redo 方法始终可以正常运行


A bit of partially relevant fact - In WPF, no matter how you change the text, the Undo and Redo methods always function properly.

这篇关于如何清除TextBox,以使默认的Undo方法仍然起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆