一个texbox控件,我想控制它的输入,1.只能输6位
2.前3个只能为字母,
3.后3位自能为数字,怎么做到?

解决方案 »

  1.   

    WebForm里有验证控件,不知道WinForm里有没有提供
      

  2.   

    用正则来进行校验
    Regex regex=new Regex(@"^\w{3}\d{3}$");
      

  3.   


     private void texbox_KeyPress(object sender, KeyPressEventArgs e)
            {
            }

    中通过代码验证限制了
      

  4.   


     private void Form1_Load(object sender, EventArgs e)
            {
                this.textBox1.MaxLength = 6;
            }        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.textBox1.Text.Length <3)
                {
                    if (char.IsLetter(e.KeyChar) == true || char.IsControl(e.KeyChar) == true)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }            }
                if (this.textBox1.Text.Trim().Length >= 3)
                {
                    if (char.IsDigit(e.KeyChar) == true || char.IsControl(e.KeyChar) == true)
                    {
                        e.Handled = false;                }
                    else
                    {
                        e.Handled = true;
                    }
                
                }
      

  5.   

    先进行输入限制如下:private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.textBox1.Text.Trim().Length == 6)
                {
                    e.Handled = true;
                }
                if (this.textBox1.Text.Trim().Length < 3)
                {
                    if ((e.KeyChar < 97 || e.KeyChar > 122)  && (e.KeyChar != 8) && (e.KeyChar != 46))
                    {
                        e.Handled = true;
                    }
                }
                else if (this.textBox1.Text.Trim().Length >=3)
                {
                    if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
                    {
                        e.Handled = true;
                    }
                }
            }然后在文本框失去焦点时用上面几位的正则来进行判断
      

  6.   

    这种情况可以考虑用 MaskedTextBox 
      

  7.   

    MaskedTextBox1.Mask = "LLL000";
      

  8.   

    改为下面的字母支持大小写private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.textBox1.Text.Trim().Length == 6)
                {
                    e.Handled = true;
                }
                if (this.textBox1.Text.Trim().Length < 3)
                {
                    if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46)))
                    {
                        e.Handled = true;
                    }
                }
                else if (this.textBox1.Text.Trim().Length >=3)
                {
                    if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
                    {
                        e.Handled = true;
                    }
                }
            }
      

  9.   

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.textBox1.Text.Trim().Length == 6)
                {
                    e.Handled = true;
                }
                if (this.textBox1.Text.Trim().Length < 3)
                {
                    if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46)))
                    {
                        e.Handled = true;
                    }
                }
                else if (this.textBox1.Text.Trim().Length >=3)
                {
                    if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
                    {
                        e.Handled = true;
                    }
                }
            }