要吗用正则表达式,要不嫌麻烦就用代码控制:
bool isNotnum = false;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
((TextBox)sender).ImeMode = ImeMode.Disable;
this.isNotnum = false;
((TextBox)sender).Tag = ((TextBox)sender).Text;
//允许输入数字、小数点和删除键
if((e.KeyChar<48||e.KeyChar>57) && e.KeyChar!=8&&e.KeyChar!=(char)('.'))
{
e.Handled = true;
this.isNotnum = true;
}
//小数点只能输入一次
if(e.KeyChar ==(char)('.')&&((TextBox)sender).Text.IndexOf('.')!=-1)
{
e.Handled = true;
}
//第一位不能为小数点
if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
{
e.Handled = true;
}
//第一位是0,第二位必须为小数点
if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
{
e.Handled = true;
}
if(e.KeyChar == 8)
{
e.Handled = false;
}
//只允许输入数字
if (e.KeyChar >= 0x4e00 && e.KeyChar <= 0x9fa5)
{
this.isNotnum = true;
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (this.isNotnum && ((TextBox)sender).Text != ((TextBox)sender).Text.ToString())
{
MessageBox.Show("只能输入数字!!!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
((TextBox)sender).Text = ((TextBox)sender).Tag.ToString();
((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
this.isNotnum = false;
return;
}
}
}
正则
[-]{0,1}\d{0,10} \d{0,16} 这两个就行了 中间是或的关系啊
加正则验证吧