如何限制用户在TextBox控件中只能输入:0.1到0.9 这9个小数中的一个。即输入的数的值是小于1的 先谢了。

解决方案 »

  1.   

    try
    {
        decimal a=convert.ToDecimal(TextBox1.Text);
        if(a<0.1 || a>0.9)
             return;
    }
    catch
    {
       return;
    }
      

  2.   

    在textBox的textChanged事件中限制输入字符为0-9 和小数点:
    private textBox_textChanged(object sender,System.EventArgs e)
    {
        for(int i=0;i<textBox1.text.length;i++)
        {
            if(textBox1.text[i]!=".")
            {
                if(!Char.IsDigit(textBox1.text[i]))
                {
                   MessageBox.Show("不能输入数字和小数点以外的任何字符","提示");
                   textBox1.text="";
                   return;
                }
            }
        }
    }
    在textBox的validating事件中检测输入值的大小:
    private void textBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
       if(textBox.text!="")
       { 
         double inputValue=Convert.toDouble(textBox.text);
         if(inputValue>0.9 || inputValue<0.1)
         {
             MessageBox.Show("输入数字应在0.1到0.9之间,请重新输入!","提示");
             textBox.SelectAll();
             return;
         }
       }
    }
    为了使当输入值不符范围时,仍能正常关闭窗体,请将窗体的causeValidation属性设置为false
      

  3.   

    你可以在TextBox的Leave事件中进行判断,
    判断当前输入是否合法,如果不合法,返回焦点进行修改
      

  4.   

    liudng(大东)   的方法我试过,消息异常回卡一下,你们的也是吗?
      

  5.   

    Regex reg = new Regex("0\\.[1-9]$");
    if( !reg.IsMatch(this.TextBox1.Text))
      this.TextBox1.Text = string.Empty;
      

  6.   


    private void textBox1_TextChanged_1(object sender, System.EventArgs e)
    {//   using System.Text.RegularExpressions;
    string pat = string.Empty;
    Regex rg = null;
    Match mh = null; if(this.textBox1.Text.Length == 1)
    {
    pat = @"^0";
    rg=new Regex(pat);
    mh=rg.Match(textBox1.Text);
    if(mh.Success)
    return;
    MessageBox.Show("不许输入  0.1--0.9  以外的字符");
    this.textBox1.Text =""; }
    else if(this.textBox1.Text.Length == 2)
    {
    pat = @"^0\.";
    rg=new Regex(pat);
    mh=rg.Match(textBox1.Text);
    if(mh.Success)
    return;
    MessageBox.Show("不许输入  0.1--0.9  以外的字符");
    this.textBox1.Text ="";
    }
    else if (this.textBox1.Text.Length > 2)
    {
    pat = @"^0.[1-9]$";
    rg=new Regex(pat);
    mh=rg.Match(textBox1.Text); //if( !rg.IsMatch(this.textBox1.Text)) if(mh.Success)
    return;
    MessageBox.Show("不许输入  0.1--0.9  以外的字符");
    this.textBox1.Text ="";
    }
    }//测试通过了
      

  7.   

    多谢大家的指点 问题解决了 小弟用了下面的方法:
    private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                
                if (textBox2.Text.Length < 3)
                {
                    if (textBox2.SelectionStart == 0)
                    {
                        if (e.KeyChar.CompareTo('0') < 0 || e.KeyChar.CompareTo('0') > 0)
                        { e.Handled = true; }
                        
                    
                    }
                    else if (textBox2.SelectionStart == 1)
                    {
                        if (e.KeyChar.CompareTo('.') < 0 || e.KeyChar.CompareTo('.') > 0)
                        { e.Handled = true; }
                        
                    }                else if (textBox2.SelectionStart == 2)
                    {
                        if (e.KeyChar.CompareTo('1') < 0 || e.KeyChar.CompareTo('9') > 0)
                        { e.Handled = true; }
                        
                    }
                }
                else
                {
                    if (e.KeyChar != '\b')
                    { e.Handled = true;
                    textBox2.Text = "";
                    }
                }
                
            }