在taxtBox怎么实现只能输入数字1-12
已经实现只能输入数字了,但是怎么设置只读1-12

解决方案 »

  1.   

    判断text是否在 1-12之间啊,正则最好
      

  2.   

    在TEXTCHANGE中直接判断就可以了
      

  3.   

    正则+1在textChange中使用正则判断
      

  4.   

    bool blnResult = Regex.IsMatch("your input", "^([1-9]|1[0-2])$");
      

  5.   

    问题看的不是很清楚
    如果说只能输入区间  [1, 12] 之间的一个整数,那么可以用正则表达式 "^([1-9]|1[0-2])$" 来判断
    如果是可以输入多个整数的话,可以用正则表达式 "^([1-9]([2-9][1-9]?|1[0-9]?)*)$" 来判断判断的事件可以采用 TextBox 提供的 Validating 事件,在验证失败的情况下可以通过 CancelEventArgs 取消文本框所发生的更改。
      

  6.   

    bool result=Regex.IsMatch(textbox1.Text.Trim(),"^0*([1-9]|1[012])$");
      

  7.   


            private void txtMonth_Validating(object sender, CancelEventArgs e)
            {
                bool blnResult = Regex.IsMatch(txtMonth.Text.Trim(), "^([1-9]|1[0-2])$");
                CancelEventArgs.Empty.ToString();        }
    是这样吗?怎么还是不行呀。
      

  8.   

    Quote: 引用 8 楼 njw1028 的回复:
      private void txtMonth_TextChanged(object sender, EventArgs e)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(txtMonth.Text, @"^\d*$")) txtMonth.Undo();
                bool blnResult = Regex.IsMatch(txtMonth.Text.Trim(), "^([1-9]|1[0-12])$");
            }
    这么写可以吗?
      

  9.   

    if(Regex.IsMatch(txtMonth.Text.Trim(), "^([1-9]|1[0-12])$"))
    {
       //dosomething
    }
    else
    {
       //dosomething
    }
      

  10.   

            private bool notInput = false;        private void txt_KeyDown(object sender, KeyEventArgs e)
            {
                notInput = false;            // Determine whether the keystroke is a number from the top of the keyboard.
                if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
                {
                    Char inputChar = (Char)e.KeyValue;
                    String strInput = this.txtThreadCount.Text + inputChar;
                    bool blnResult = Regex.IsMatch(strInput, "^([1-9]|1[0-2])$");
                    if (blnResult == false)
                    {
                        notInput = true;
                    }
                }        }        private void txt_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (notInput == true)
                {
                    e.Handled = true;
                }        }
      

  11.   


    不可以指的是什么,blnResult 的结果不正确,还是说文本框里的内容没取消更改?CancelEventArgs 那句不明白 lz 是想做什么,如果是想取消更改,应该是 e.Cancel = true 才对。
      

  12.   


    Validating 是 TextBox 控件的一个验证事件,你查下开发文档就知道了。
    里面那个正则表达式是验证文本框的文本格式。这些内容多查下文档就都知道了。