窗体中有一个用户控件,用户控件有个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;
}
}
代码如下:
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;
}
}
{
xxxxxx;
}
flag为true时
textBox_fpb1.Leave += new System.EventHandler(textBox_fpbl_Leave);flag为false时
textBox_fpb1.Leave -= new System.EventHandler(textBox_fpbl_Leave);可以在flag的set方法里面设定。
这种方式肯定不对。把这个事件给去掉了,后面有可能继续修改textbox的值,还需要继续对他进行校验
textBox_fpb1.Leave += new System.EventHandler(textBox_fpbl_Leave);
换个方法
private void textBox1_Validating(object sender, CancelEventArgs e)
{
string text = textBox1.Text.Trim(); if (text == string.Empty)
{
MyTip.WarnTip("业绩比例不能为空!");
e.Cancel = true;
}
}
你在设置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;
}
}