c# TextBox只能输入数字的处理方法(完整版各种情况考虑在内,可根据需求灵活修改)

     //选择文本框的事件窗口,找到按键输入的方法KeyPress,双击建立新的方法。
        /// <summary>
        /// textBox只能输入数字的处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            //判断按键是不是要输入的类型。
            var textBox1 = (TextBox)sender;
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46 && e.KeyChar != 0x2D)
                e.Handled = true;
            //处理负数  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length != 0)) e.Handled = true;
            //处理0(如果第一位为0且不是全选的情况的话只能输入小数点或者退格键)
            if ((int)e.KeyChar != 46 && e.KeyChar != '' && textBox1.SelectionLength != textBox1.TextLength)
            {
                //分正负数两种情况
                if (textBox1.TextLength == 1 && textBox1.Text.Substring(0, 1) == "0")
                {
                    e.Handled = true; 
                }
                else if (textBox1.TextLength == 2 && textBox1.Text.Substring(0) == "-0")
                {
                    e.Handled = true; 
                }
            }
            
            //小数点的处理。
            if ((int)e.KeyChar == 46) //小数点
            {
                if (textBox1.Text.Length <= 0 || textBox1.Text.LastIndexOf('.') != -1)
                    e.Handled = true; //小数点不能在第一位(正数)或不能有多个小数点
                else if(textBox1.TextLength==1 && textBox1.Text.Substring(0) == "-")
                {
                    e.Handled = true; //小数点不能在第一位(负数)
                }
            }
        }
输入值是数字(含小数)
     /// <summary>
        /// 判断输入值是否符合要求(含小数)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
        {
            var textBox1 = (TextBox)sender;
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数 
            //处理0(如果第一位为0且不是全选的情况的话只能输入小数点或者退格键)
            if ((int)e.KeyChar != 46 && e.KeyChar != '' && textBox1.SelectionLength != textBox1.TextLength)
            {
                //分正负数两种情况
                if (textBox1.TextLength == 1 && textBox1.Text.Substring(0, 1) == "0")
                {
                    e.Handled = true;
                }
                else if (textBox1.TextLength == 2 && textBox1.Text.Substring(0) == "-0")
                {
                    e.Handled = true;
                }
            }
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符  
                }
            }
        }
正整数
     /// <summary>
        /// 判断输入值是否符合要求(正整数)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void tbx_Int_KeyPress(object sender, KeyPressEventArgs e)
        {
            var textBox1 = (TextBox)sender;
            if (e.KeyChar == 0x20 || e.KeyChar == 0x2D || (int)e.KeyChar == 46) e.KeyChar = (char)0;  //禁止空格键和负数/小数   
            if (
                ((textBox1.TextLength == 0 || textBox1.TextLength==textBox1.SelectionLength) && (int)e.KeyChar == 48)
                || (textBox1.TextLength == 1 && textBox1.Text=="0" && textBox1.TextLength != textBox1.SelectionLength)
                )
                e.Handled = true;//0的处理
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符  
                }
            }
        }
48代表0,57代表9,8代表空格,46代表小数点 ,0x2D代表负数, ''代表退格键
第一种代码考虑了各种情况,如不需要可以去除部分代码实现更简单的效果,例如如果不需要负数的就可以把所有关于负数判断的去掉,灵活运用
原文地址:https://www.cnblogs.com/zhoushuang0426/p/10470349.html