C#限制TextBox只能输入数字
1、以价格录入为例:
新建一个窗口,在上面放一个TextBox,命名为txtPrice;
2、在属性事件中找到KeyPress事件,编写代码如下:
private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{ //限制只能输入数字,Backspace键,小数点
if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.')
{
e.Handled = true; //非以上键则禁止输入
}
if (e.KeyChar == '.' && txtDownPrice.Text.Trim() == "") e.Handled = true; //禁止第一个字符就输入小数点
if (e.KeyChar == '.' && txtDownPrice.Text.Contains(".")) e.Handled = true; //禁止输入多个小数点
}
3、保存运行试试吧!
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:77
阅读量:137
阅读量:42
阅读量:159
阅读量:46