zoukankan      html  css  js  c++  java
  • C#设置textBox只能输入数字(正数,负数,小数)简单实现

    /*
           *设置textBox只能输入数字(正数,负数,小数)
           */
            public static bool NumberDotTextbox_KeyPress(object sender, KeyPressEventArgs e)
            {
                //允许输入数字、小数点、删除键和负号
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
                {
                    return true;
                }
                if (e.KeyChar == (char)('-'))
                {
                    if ((sender as TextBox).Text != "")
                    {
                        return true;
                    }
                }
                //小数点只能输入一次
                if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
                {
                    return true;
                }
                //第一位不能为小数点
                if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
                {
                    return true;
                }
                //第一位是0,第二位必须为小数点
                if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
                {
                    return true;
                }
                //第一位是负号,第二位不能为小数点
                if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
                {
                    return true;
                }
    
                return false;
            }
    
            public static bool NumberTextbox_KeyPress(KeyPressEventArgs e)
            {
                if (e.KeyChar != '')//这是允许输入退格键
                {
                    if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                    {
                        return true;
                    }
                }
    
                return false;
            }
    

      

  • 相关阅读:
    SpringMVC
    SpringMVC
    SpringMVC
    Spring
    Spring
    值类型和引用类型
    判断字符串的开头和结尾
    二分法(课后)
    验证码
    从1-36中随机出6个不相等的数
  • 原文地址:https://www.cnblogs.com/jhlong/p/5715043.html
Copyright © 2011-2022 走看看