艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

限制文本框只能输入数字或者小数点

if (Convert.ToInt32(e.KeyChar) == 8)
            {
                e.Handled = false;
            }
            else
            {
                Regex rex = new Regex(@"^[0-9.]*$"); //初始化正则表达式(检测每次输入的字符)
                Regex rexFull = new Regex(@"^[0-9]+(.[0-9]{0,1})?$"); //初始化正则表达式(检测所有已经输入的字符)
                if (rexFull.IsMatch(this.txtMoney.Text.Trim()) || rexFull.IsMatch(this.txtMoney.Text.Trim() + e.KeyChar.ToString()))
                {
                    if (Regex.Matches(this.txtMoney.Text.Trim() + e.KeyChar.ToString(), "\\.").Count == 2)  //防止输入两个小数点
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        if (!(char.IsNumber(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == (char)('.')))
                        {
                            e.Handled = true;
                        }
                        else

                        {
                            e.Handled = false;
                        }
                    }
                }
                else

                {
                    e.Handled = true;
                }
            }