怎样实现一个TextBox,只接受数字输入,并且限制最大和最小值?
fifa 2003-07-02 03:11:22 怎样实现一个TextBox,只接受数字输入,并且限制最大和最小值?
我按下面方式做时,发生死循环。高分求购解决方法。
class DigitalBox : TextBox;
// 排斥非数字输入
protected override void OnKeyPress(KeyPressEventArgs e)
{
if ( e.KeyChar >= '0' && e.KeyChar <= '9' )
{
string str = (string)this.Text.Clone();
base.OnKeyPress( e );
int v = int.Parse(this.Text);
if ( v < minValue || v > maxValue )
{
this.Text = str;
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}