忘了在文本框的keypress事件中如何实现上述功能?请教
private void ucNumberTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//8 回车
if((int)e.KeyChar != 8)
{
if(((int)e.KeyChar != 46) && ((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 13)
{
e.Handled = true;
}
//允许小数
if(_decimalLength > 0)
{
int x = this.Text.IndexOf(.);
//限制整数
if(x < 0 && this.Text.Length >= _intLength && (int)e.KeyChar != 46)
{
e.Handled = true;
}
//超过整数位置只能输入小数
else if(x > _intLength + 1 && (int)e.KeyChar != 46)
{
e.Handled = true;
}
else if(x >= 0 && ((int)e.KeyChar == 46 || (this.Text.Substring(x).Length) > _decimalLength))
{
e.Handled = true;
}
}
//只能是整数
else
{
if(this.Text.Length >= _intLength || (int)e.KeyChar == 46)
{
e.Handled = true;
}
}
if(this.Text.Length < 1)
{
if((int)e.KeyChar == 46)
e.Handled = true;
}
}
}