我做了一个文本框。怎么锁定,输入法,只让用户输入123456789,不能输入其它 的

解决方案 »

  1.   

    在TextChanged事件中^[0-9]{1,}$ 正则表达式   
    Regex r = new   Regex("^[0-9]{1,}$");   
    if(!r.IsMatch(textBox1.Text))   
    {   
          Messagebox.Show("请输入数字");   
    }
      

  2.   

     private void textBoxAddWrite_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ('0' > e.KeyChar || e.KeyChar > '9')//输入的按键值在‘0’~‘9’之间
                {
                    if (e.KeyChar != 8)//不是backspace键
                        e.KeyChar = (char)0;//设定键值为0
                }
      

  3.   

    KEYPRESS事件里写: 
         Private Sub txt_BXPDJ02009_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    ' 0キーから9キーまでと、BSキー以外の場合
     If Not ((Strings.Asc("0") <= Strings.Asc(e.KeyChar) And Strings.Asc(e.KeyChar) <= Strings.Asc("9")) Or _
                                Strings.Asc(e.KeyChar) = 8 Or Strings.Asc(e.KeyChar) = Strings.Asc("-")) Then                        Messagebox.Show("请输入数字");  
                        End If
    end sub
      

  4.   

    用正则表达式或用KeyPress和KeyDown事件控制
      

  5.   

    给你一个参考下吧private void textBox_KeyDown(object sender, KeyEventArgs e)
            {
                if ((e.KeyValue >= 48 && e.KeyValue <= 57) || e.KeyValue == 8 || e.KeyValue == 46 || e.KeyValue == 190)
                {
                    if (_FormatMode == FilterMode.Number)
                    {
                        if (e.KeyValue == 190)
                        {
                            isValidate = false;
                            isEnter = false;
                        }
                        else
                        {
                            isValidate = true;
                            isEnter = false;
                        }
                    }
                    else if (_FormatMode == FilterMode.NumberAndDot)
                    {
                        isValidate = true;
                        isEnter = false;
                    }
                    else
                    {
                        isValidate = false;
                        isEnter = false;
                    }
                }
                else if (e.KeyValue == 13)
                {
                    isValidate = true;
                    isEnter = true;                decimal tmp = Convert.ToDecimal(this.textBox.Text.Trim());
                    if (tmp <= _MinNumber)
                        tmp = _MinNumber;
                    else if (tmp >= _MaxNumber)
                        tmp = _MaxNumber;                this.textBox.Text = tmp.ToString(_FormatString);
                }
                else
                {
                    isValidate = false;
                    isEnter = false;
                }            base.OnKeyDown(e);
            }        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (isValidate)
                {
                    e.Handled = false;
                }
                else
                    e.Handled = true;
                if (isEnter)
                {
                    try
                    {
                        Convert.ToDecimal(this.textBox.Text.Trim());
                    }
                    catch
                    {
                        this.textBox.Text = "0";
                    }                decimal tmp = Convert.ToDecimal(this.textBox.Text.Trim());
                    if (tmp <= _MinNumber)
                        tmp = _MinNumber;
                    else if (tmp >= _MaxNumber)
                        tmp = _MaxNumber;                this.textBox.Text = tmp.ToString(_FormatString);
                }            base.OnKeyPress(e);
            }