主要是重写OnKeyPress事件,如果输入不正确就不接收字符。


主要是重写OnKeyPress事件,如果输入不正确就不接收字符。


protected override void OnKeyPress(KeyPressEventArgs e)

{

    //base.OnKeyPress(e);

    //如果是退格键

    if (e.KeyChar == (char)Keys.Back)

    {

          return;

    }

    //e.Handled = true;//不接受输入

    double outDb=0;

    if (double.TryParse(this.Text + e.KeyChar.ToString(), out outDb))

    { 

        e.Handled = false;

    }

    else

    {

        e.Handled = true;

    }

}