Winform TextBox中只能输入数字的几种常用方法(C#)

方法一:  
  
private void tBox_KeyPress(object sender, KeyPressEventArgs e)  
  
 {  
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数  
            if (e.KeyChar > 0x20)  
            {  
                try  
                {  
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());  
                }  
                catch  
                {  
                    e.KeyChar = (char)0;   //处理非法字符  
                }  
            }  
}  
  
方法二:  
  
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)  
 {  
    if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))  
    {  
      e.Handled = true;  
    }  
}  
或者  
  
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)  
{  
    if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))  
    {  
      e.Handled = true;  
    }  
  
}  
  
方法三:  
  
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
{  
if(e.KeyChar!='\b')//这是允许输入退格键  
{  
if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字  
{  
e.Handled = true;  
}  
}  
}  
  
方法四:  
  
private void textBox1_Validating(object sender, CancelEventArgs e)   
{   
const string pattern = @"^\d+\.?\d+{1}quot;;   
string content = ((TextBox)sender).Text;   
  
if (!(Regex.IsMatch(content, pattern)))   
{   
errorProvider1.SetError((Control)sender, "只能输入数字!");   
e.Cancel = true;   
}   
else   
errorProvider1.SetError((Control)sender, null);   
}  
  
方法五:  
  
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
{  
if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)  
{  
e.Handled=true;  
}  
  
if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))  
{  
e.Handled=true;  
}  
  
}  
  
方法六:  
  
private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)  
{  
            if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))  
            {  
                e.Handled = true;//消除不合适字符  
            }  
            else if (Char.IsPunctuation(e.KeyChar))  
            {  
                if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点  
                {  
                    e.Handled = true;  
                }  
                if (textBox1.Text.LastIndexOf('.') != -1)  
                {  
                    e.Handled = true;  
                }  
            }        
  }    
  
方法七:  
  
利用ASCII码处理办法、  
{  
  
            if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))  
              e.Handled = true;  
================48代表0,57代表9,8代表空格,46代表小数点  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值