1
private
void
txtStr_KeyPress(
object
sender, System.Windows.Forms.KeyPressEventArgs e)
2 {
3 if ( ! char .IsNumber(e.KeyChar) && ( short )e.KeyChar != 44 && ( short )e.KeyChar != 8 && ( short )e.KeyChar != 13 )
4 e.Handled = true ;
5 }
6
2 {
3 if ( ! char .IsNumber(e.KeyChar) && ( short )e.KeyChar != 44 && ( short )e.KeyChar != 8 && ( short )e.KeyChar != 13 )
4 e.Handled = true ;
5 }
6
这使用了char.IsNumber来判断是否输入的为数字,而其它几个使用了ASCII来判断。当然你也可以将char.IsNumber改用ASCII来判断。