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、保存运行试试吧!