关键新闻事件不起作用 [英] key press event not working

查看:63
本文介绍了关键新闻事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了按键事件的问题。当我在textbox1中输入10位数时,它需要10位数。但是当我清除textbox1按退格键然后再输入数字时,它只需9位数就是我的问题。请告诉我我的代码出现了什么问题,如

I have problem with keypress event. when i enter 10 digits in textbox1 it takes 10 digits as i want. but when i clear textbox1 pressing backspace and now enter digits again it takes only 9 digits that is my problem. please tell me what's going wrong with my code is given as under

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar != '\b' && textBox4.Text.Trim().Length > 9)
    {
        //here '\b' is for backspace
        e.Handled = true;
        MessageBox.Show("You can't enter more than ten digits...");
        textBox4.MaxLength = 9;
    }
}

推荐答案

在if语句中,将MaxLength设置为9,因此它只接受9个字符。您不必更改此值。
Inside your if statement, you set the MaxLength to 9, so it only accepts 9 chars. You don't have to change this value.


输入第十个数字后( textBox4.Text.Trim()。长度> 9 )你设置 textBox4.MaxLength = 9;



这就是为什么第一次10工作,但在那之后只有9
Once the tenth digit is entered (textBox4.Text.Trim().Length > 9) you are setting the textBox4.MaxLength = 9;

That is why the first time 10 works, but after that only 9


这篇关于关键新闻事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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