首先绑定keypresss事件。然后调用这句话就ok了
private static bool verifyFloatNumberInputPressCallBack(object sender, KeyPressEventArgs e)
{
string text = ((TextBox)sender).Text;
if (e.KeyChar == '\b')
{
LogUtil.writeLog("退格键");
return false;
}
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;//不合法
LogUtil.writeLog("非数字键也不是小数点,忽略");
return false;
}
text = text.Insert((int)text.Length, e.KeyChar.ToString() + "0");
LogUtil.writeLog("input {0}", text);
if (!RegHelper.isNumber(text) && e.KeyChar != '\b' && text != "")
{
LogUtil.writeLog("不合法");
e.Handled = true;//不合法
return false;
}
return true;
}