要求,在两个TEXTBOX里面输入东东,只能输入数字,删除键,回车键,字母B.
代码如下;private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
             if (e.KeyChar == 8 || e.KeyChar == 65 || e.KeyChar == 97 || char.IsDigit(e.KeyChar)||e.KeyChar == (char)13)
                   {
                     return;
            }             if (e.KeyChar == (char)13)
             {
                 this.SelectNextControl(this.ActiveControl, true, true, false, false);
             }
            else
            {
                label1.Text = "输入有误请重新输入";
                e.Handled = true;
            }
        }
现在的问题是,不管你怎么输入,他都会提示输入有误请重新输入";
而且按回车键,焦点也不会回到下一个控键.

解决方案 »

  1.   

    首先,e.KeyChar == 8,这个可以吗,应该‘b’;
    if (e.KeyChar == (char)8 || e.KeyChar == (char)65 || e.KeyChar == (char)97 || char.IsDigit(e.KeyChar) || e.KeyChar == (char)13)
                {
                    if (e.KeyChar == (char)13)
                    {
                        this.SelectNextControl(this.ActiveControl, true, true, false, false);
                    }
                }
                else
                {
                    label1.Text = "输入有误请重新输入";
                    e.Handled = true;
                }
      

  2.   


     private void textBox1_TextChanged(object sender, EventArgs e)
            {
                string text = textBox1.Text;
                string pattern = @"^[B0-9\r\n]*$";//字母B,数字,回车换行
                Match m = Regex.Match (text, pattern);
                if (m.Success)
                {
                    label1.Text = "";
                }
                else {
                    label1.Text = "错误";
                }
            }