小数点后有两位,整数部分有六位

解决方案 »

  1.   


                Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$");
                if (!reg.IsMatch(textBox1.Text.Trim()))
                {
                    MessageBox.Show("请输入正确的数字");
                    textBox1.Focus();
                    return;
                }
                else
                {
                    MessageBox.Show("输入正确!");
                }
      

  2.   


                Regex regex = new Regex(@"^\s*?\d{1,6}\.[\d]{1,2}\s*?$");
                if (!regex.IsMatch(textBox1.Text))
                {
                    MessageBox.Show("Error");
                    textBox1.SelectAll();
                    return;
                }
                Console.WriteLine(textBox1.Text.Trim());
      

  3.   

    以下代码可以禁止输入非要求的其他内容(放在KeyDown事件中),模式检测可以在TextChanged事件中,如1,2楼代码
                if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                    || (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
                    || e.KeyCode == Keys.OemPeriod
                    || e.KeyCode == Keys.Back)
                {
                   
                }
                else
                {
                    e.SuppressKeyPress = true;
                }
      

  4.   

    顶楼上,在KeyDown事件中判断,通过e.KeyValue得到按键值,判断是否是数字或者小数点,如果不是在keyUp事件中清楚达不到要求的文本
      

  5.   

    用自带NumericUpAndDown岂不是更好,更方便?
      

  6.   

    如果我输入2.0.34.56呢? 这咋办?1楼2楼的也是欠缺考虑的.写的不够严谨. 没有完全达到楼主要求.正则太浪费计算了. 建议还是直接写逻辑判断吧. string last = "";//记录最后一次输入符合要求的数字
            decimal max = new decimal(999999.99);//设置最大可输入
            decimal min = new decimal(0);//设置最小可输入
            int dot = 2;//小数点之后保留2位        private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (this.textBox1.Text == last)
                    return;            if (this.textBox1.Text != null)
                {
                    decimal d;
                    if (decimal.TryParse(this.textBox1.Text, out d))
                    {
                        if (d > max || d < min)
                        {
                            MessageBox.Show("数字只能在" + min + " - " + max + "之间(包括" + min + "和" + max + ").");
                            this.textBox1.Text = last;
                            return;
                        }
                        int p = this.textBox1.Text.IndexOf('.');
                        if (p != -1 && p < this.textBox1.Text.Length - dot -1)
                        {
                            MessageBox.Show("小数最多2位");
                            this.textBox1.Text = last;
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("只能输入数字");
                        this.textBox1.Text = last;
                        return;
                    }            }
                last = this.textBox1.Text;
            }如果懒的话,直接系统控件 NumericUpDown
    DecimalPlaces = 2;//小数位数为2
    Maximum = new decimal(999999.99);//最大数字为6位整数+2位小数的9
      

  7.   

    decimal number = 0;
    if (decimal.TryParse(strCharge, out number) == true)
    {}
    else
    {}最好写在textbox的valided事件中