现在的位置: 首页 > 综合 > 正文

只能输入数字的TextBox自定义控件

2013年01月25日 ⁄ 综合 ⁄ 共 504字 ⁄ 字号 评论关闭
主要是重写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;
    }
}

url:http://greatverve.cnblogs.com/archive/2012/07/03/NumberTextBox.html
源码:http://files.cnblogs.com/greatverve/WinControl.rar

抱歉!评论已关闭.