简体   繁体   中英

Backspace only works once?

I've got a custom TextBox-like control. 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. 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??? This is the output I get when I backspace twice:

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.

As it turns out, the '\b' character was getting appended to my text after pressing the backspace key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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