一个只能输入数字的TEXTBOX

来源:互联网 发布:手机微信一键群发软件 编辑:程序博客网 时间:2024/05/18 16:18

public class TextBoxNum : System.Windows.Forms.TextBox
 {
  public TextBoxNum()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
  {
   if(e.KeyChar.ToString().Length==1)
   {
    switch(e.KeyChar)
    {
     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9':
     case (char)System.Windows.Forms.Keys.Back://可以扩展其他键值的有效性
      e.Handled = false;
      break;
     default:

//其他键值都不允许
      e.Handled = true;
      break;

    }
   }
   base.OnKeyPress (e);
  }
  protected override void OnImeModeChanged(EventArgs e)
  {

//屏蔽输入法
   this.ImeMode = System.Windows.Forms.ImeMode.Disable;
   base.OnImeModeChanged (e);
  }


 }

原创粉丝点击