两个相连textbox本来按enter就能从textbox1到textbox2,现在要焦点离开时校验text的字串长度是不是=4,如果不是=4焦点就不能离开textbox。
如果只是textbox1用这个事件就行,但是textbox1和textbox2都用的话,就会在textbox1那里循环出messagebox的内容,点完ok后,焦点会跑到textbox2,
        private void text1_lostfocus(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.Length != 4)
            {
                MessageBox.Show("位数有误");
                tb.Focus();
            }
        }能不能textbox1长度不符合=4的条件,不能跳出textbox1,而且messagebox的内容只出一次。

解决方案 »

  1.   

    把tb.Focus(); 改成e.handle=TRUE
      

  2.   

    不知道wince下有没有Validating事件?如果有的话,使用此事件验证。
      

  3.   

    如果在这个事件的处理方法中,设置验证无效的情况下e.Cancel=true;那么,光标是跳不出这个TextBox的。
      

  4.   

    Validating事件试过了,也不行,OhYeah_Dragon说的只是改就可以了?
    我的是这样的,
    this.textBox2Frm4.LostFocus += new System.EventHandler(this.text1_lostfocus);
      

  5.   

    值得一提的是,我另外一个form里面这样做是可以的        
    this.textBox1.LostFocus += new System.EventHandler(this.lost1);         
    private void lost1(object sender, EventArgs e)
            {
                if (this.textBox1.Text.Length != 4)
                {
                    MessageBox.Show("位数有误");
                    this.textBox1.Focus();
                }
            }this.textBox2.LostFocus += new System.EventHandler(this.lost2);
            private void lost2(object sender, EventArgs e)
            {
                if (this.textBox2.Text.Length != 4 && this.textBox2.Text.Length != 0)
                {
                    MessageBox.Show("位数有误");
                    this.textBox2.Focus();
                }
            }
      

  6.   

    Validating和lostfocus的出错情况一样
      

  7.   

    把你的Validating事件处理方法贴出来。
      

  8.   

    this.textBox2Frm4.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2Frm4_Validating);
    this.textBox3Frm4.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2Frm4_Validating);        private void textBox2Frm4_Validating(object sender, CancelEventArgs e)
            {
                TextBox tb = (TextBox)sender;
                if (tb.Text.Length != 4)
                {
                    MessageBox.Show("位数有误");
                    tb.Focus();
                }
            }
      

  9.   

    另外一个实验,
    在textBox2Frm4和textBox3Frm4不能连用,但是如果textBox2Frm4和textBox4Frm4用就正常
      

  10.   

    在Validating事件处理方法中,不要使用什么Focus方法,设置e.Cancel=true就可以了。
      

  11.   

     private void text1_lostfocus(object sender, EventArgs e)
            {
                TextBox tb = (TextBox)sender;
                if (tb.Text.Length != 4)
                {
                    tb.Focus();
                    MessageBox.Show("位数有误");//把他们换一下位置试试
                    
                }
            }