简体   繁体   English

退格只能使用一次?

[英]Backspace only works once?

I've got a custom TextBox-like control.我有一个自定义的类似 TextBox 的控件。 I'm trying to handle backspaces with this:我正在尝试用这个来处理退格:

    private void PerformBackspace() {
        System.Diagnostics.Debug.WriteLine("Got a backspace. Text is currently \"{0}\".", (object)this.Text);
        string newText = this.Text.Substring(0, this.Text.Length - 1);
        System.Diagnostics.Debug.WriteLine("Changing text to \"{0}\".", (object)newText);
        this.Text = newText;
        System.Diagnostics.Debug.WriteLine("Text is now \"{0}\".", (object)this.Text);
    }

In OnKeyDown I call this method, and it works, but only for one character.OnKeyDown中,我调用了这个方法,它有效,但仅适用于一个字符。 Backspacing twice in a row does not work, you have to type at least one character between backspaces for some reason.连续两次退格不起作用,出于某种原因,您必须在退格之间输入至少一个字符。 Now is Substring just not working???现在Substring不工作??? This is the output I get when I backspace twice:这是我退格两次时得到的 output:

Got a backspace.有退格键。 Text is currently "My textbox.".文本当前是“我的文本框”。
Changing text to "My textbox".将文本更改为“我的文本框”。
Text is now "My textbox".文本现在是“我的文本框”。
Got a backspace.有退格键。 Text is currently "My textbox".文本当前是“我的文本框”。
Changing text to "My textbox".将文本更改为“我的文本框”。
Text is now "My textbox".文本现在是“我的文本框”。

This is quite possibly the strangest issue I have ever seen.这很可能是我见过的最奇怪的问题。

Try calling it from the OnKeyUp() event.尝试从 OnKeyUp() 事件中调用它。

As it turns out, the '\b' character was getting appended to my text after pressing the backspace key.事实证明,在按下退格键后, '\b'字符被附加到我的文本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM