我如何知道在文本框的 TextChanged 事件中是否按下了退格键或删除键

发布于 2024-11-08 18:41:27 字数 178 浏览 0 评论 0原文

我使用文本框的 TextChanged 事件。

例如我的文本框中有文本: "a b"- a,b 之间有 2 个空格 我走到两个空格的中间,然后按退格键。我无法通过将新文本与旧文本进行比较来检查按下了哪个键(如果按下了退格键或删除键,则新文本是 字母之间有相同的“a b”-1 空格。

如何检查按下的是哪个键?

I use the TextChanged event of a textbox.

for instance I have the text in the textbox:
"a b"- 2 spaces between a,b
I go in the middle of the 2 spaces, and hit backspace. I can not check by comparing the new text with the old which key was pressed (if backspace or delete was pressed, the newtext is the
same "a b"-1 space between letters.

How can I check which key was pressed?

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

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

发布评论

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

评论(2

蓝眼睛不忧郁 2024-11-15 18:41:27

你为什么关心?如果您正在处理 TextChanged 事件,则不必关心文本发生了哪些变化,而只关心文本发生了变化这一事实。

如果您需要关心具体更改的内容,则需要处理较低级别的事件,例如 KeyDown

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Back || e.Key == Key.Delete)
    {
        // The user deleted a character
    }
}

Why do you care? If you're handling the TextChanged event, you're not supposed to care what about the text was changed, just the fact that it was.

If you need to care about what specifically was changed, you need to handle a lower-level event, like KeyDown:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Back || e.Key == Key.Delete)
    {
        // The user deleted a character
    }
}
〃温暖了心ぐ 2024-11-15 18:41:27

我知道这是一个老问题,但也许有人会发现这很有帮助。

我使用了一个整数来计算当前字符的长度。它随事件更新。
每次触发事件时,我都会检查计数器的长度是否比文本的当前长度长。如果是的话,我们知道,有一些东西被删除了。

    private int counter = 0;

private void TextChangedEvent(object sender, TextChangedEventArgs e){

if(counter > textbox.text){
counter = text.Length;
//deleted
}

else{
counter = text.Length;
//added}

I know this is an old question, but maybe someone will find this helpful.

I used an integer, that counts length of current characters. It updates with the event.
Every time, the event is triggered, I check, if the length of the counter is longer than the current length of the text. If it is, we know, there was something deleted.

    private int counter = 0;

private void TextChangedEvent(object sender, TextChangedEventArgs e){

if(counter > textbox.text){
counter = text.Length;
//deleted
}

else{
counter = text.Length;
//added}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文