Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中按backspace时的TextBox按键事件#_C#_Keypress_Backspace - Fatal编程技术网

C# C中按backspace时的TextBox按键事件#

C# C中按backspace时的TextBox按键事件#,c#,keypress,backspace,C#,Keypress,Backspace,它应该删除字符串的最后一个字符 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { do { 本节在文本框中写入字符 if (e.KeyChar >= 48 && e.KeyChar <= 57) { textBox1.Text += e.KeyChar.

它应该删除字符串的最后一个字符

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        do
        {
本节在文本框中写入字符

            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                textBox1.Text += e.KeyChar.ToString();
                textBox1.SelectionStart = textBox1.Text.Length;
            }

此代码中的问题是,在更改字符串时,没有为其赋值。这是因为
String.Remove()
返回一个字符串,而不是直接更改字符串

sssnumber.Remove(sssnumber.Length - 1);
将删除字符串末尾的字符,然后处理结果

sssnumber = sssnumber.Remove(sssnumber.Length - 1);

另一方面,将执行相同的操作,但随后将
sssnumber
的值设置为删除的值。

我建议您使用KeyDown事件,它提供了更多的控制。如果使用了退格,你能不能不按键盘返回,从而保留按键的实际功能?@AdrianManuelCleofe很高兴我能帮上忙!如果答案对你有用,你可以接受
sssnumber = sssnumber.Remove(sssnumber.Length - 1);