C#高手进来下 帮我解释下这段代码的意思 谢谢

来源:百度知道 编辑:UC知道 时间:2024/06/19 23:52:12
private void txtAge_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true; //获取或设置一个值,指示是否处理过System.Windows.Forms.Control.KeyPress事件
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar == '.')
{
if (((TextBox)sender).Text.LastIndexOf('.') != -1)
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}

}
我只知道这段是限制文本框输入的 实现文本框只能输入数字的代码 帮我解释一下他是怎么实现的 和具体的信息。。。 就是具体的解释一下

关键在于这一句
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))

翻译成中文就是
if(输入的不是数字 and 输入的不是标点 and 输入的不是Ctrl键)
{
e.Handled = true; //告诉程序已经处理了.
那么程序就不会把这个字显示出来.因为你告诉程序已经处理了
}
if()//下面不说了.一样的.限制输入标点只能输小数点.否则也当成处理过了

楼上的没有考虑输入小数点的情况哦.人家的判断是有目的的.

其实
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar))
{
//如果不是数字
e.Handled = true; //取消
}
}
这样写就得了

上面是哪个家伙写的啊~烂哩

他们说的不对