窗体中有一个用户控件,用户控件有个textbox,textbox的leave事件,该事件先弹出一个消息框,然后会执行textbox.Focus()方法。现在的问题是 如果我把焦点移到用户控件内部的其他控件上,很正常。如果焦点移到用户控件之外的控件上,会一直执行leave事件,陷入死循环。可能是因为textbox没有获取焦点。请问该如何解决?
代码如下:
private void textBox_fpbl_Leave(object sender, EventArgs e)
        {
            string text = textBox_fpbl.Text.Trim();            if (text == string.Empty)
            {
                MyTip.WarnTip("业绩比例不能为空!");
               
                textBox_fpbl.Focus();
                return;
            }
        }

解决方案 »

  1.   

    是的啊你用的textBox_fpbl_Leave事件啊,当textBox_fpbl不是活动的时候就会触发啊,我觉得你应该用MouseLeave事件
      

  2.   

    if(textbox.focused && text == string.empty)
    {
       xxxxxx;
    }
      

  3.   

    当用户控件focus时 设置一个flag为true用户控件中
    flag为true时
    textBox_fpb1.Leave += new System.EventHandler(textBox_fpbl_Leave);flag为false时
    textBox_fpb1.Leave -= new System.EventHandler(textBox_fpbl_Leave);可以在flag的set方法里面设定。
      

  4.   

    textBox_fpb1.Leave -= new System.EventHandler(textBox_fpbl_Leave);
    这种方式肯定不对。把这个事件给去掉了,后面有可能继续修改textbox的值,还需要继续对他进行校验
      

  5.   

    上面的代码已经写了,当你 user控件获得焦点的时候,flag为true时
    textBox_fpb1.Leave += new System.EventHandler(textBox_fpbl_Leave);
      

  6.   

    不好意思,审错题了,上面写的不对~~你在userControl的Leave事件里面进行一下,内部控件check(设置一个变量传到UserControl,在UserControl里面进行check),如果没错,继续执行。如果出错,焦点设置到userControl。不知道还有没有更方便的方法。
      

  7.   

    间接性死循环,比方说楼主的程序是个mdi,会造成程序卡死(但cpu不高);
    换个方法
    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
                string text = textBox1.Text.Trim();            if (text == string.Empty)
                {
                   MyTip.WarnTip("业绩比例不能为空!");
                    e.Cancel = true;
                    
                }
    }
      

  8.   

    userControl
    你在设置TextBox焦点前,加上一句,this.Focus();
    看看可不可以。
    private void textBox_fpbl_Leave(object sender, EventArgs e)
            {
                string text = textBox_fpbl.Text.Trim();            if (text == string.Empty)
                {
                    MyTip.WarnTip("业绩比例不能为空!");
                    this.Focus();
                    textBox_fpbl.Focus();
                    return;
                }
            }
      

  9.   

    遇到同样的问题,求解救,,,,help,,,搞了半天