zoukankan      html  css  js  c++  java
  • c#textBox控件限制只允许输入数字及小数点

    在textboxd的事件中的 KeyPress 事件,这样双击进入代码:输入以下代码 即可

    //判断按键是不是要输入的类型。
    
                if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
    
                    e.Handled = true;
    
    
                //小数点的处理。
    
                if ((int)e.KeyChar == 46)                           //小数点
    
                {
    
                    if (textBox1.Text.Length <= 0)
    
                        e.Handled = true;   //小数点不能在第一位
    
                    else
    
                    {
    
                        float f;
    
                        float oldf;
    
                        bool b1 = false, b2 = false;
    
                        b1 = float.TryParse(textBox1.Text, out oldf);
    
                        b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
    
                        if (b2 == false)
    
                        {
    
                            if (b1 == true)
    
                                e.Handled = true;
    
                            else
    
                                e.Handled = false;
    
                        }
    
                    }
    
                }

     处理只输入数字的:

    方法一:  
      
    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!=''&&!Char.IsDigit(e.KeyChar))  
        {  
          e.Handled = true;  
        }  
      
    }  
      
    方法三:  
      
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
    {  
    if(e.KeyChar!='')//这是允许输入退格键  
    {  
    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代表小数点  
    }
    View Code
  • 相关阅读:
    Node Exporter监控指标
    Prometheus组件介绍
    记录阿里云安全组设置遇到的奇葩问题--出口ip
    7.prometheus监控多个MySQL实例
    使用Docker Compose部署SpringCloud项目docker-compose.yml文件示例
    Docker Compose的安装及命令补全
    如何调试 Docker
    Dockerfile 最佳实践
    Docker 命令查询
    Docker常见问题
  • 原文地址:https://www.cnblogs.com/liuqifeng/p/9148801.html
Copyright © 2011-2022 走看看