我的应用背景是这样的:
在一个Form上有很多控件,其中有一个是text控件,一个是button控件。 当焦点从该text控件离开时会做检查,如果检查失败,会再把焦点设置到该text控件上。
当点了button控件时,想直接关闭窗体。但是,如果焦点是在text上,我直接点button控件时该button控件就不起作用了。有没有办法判断我是在点了button控件后出发了text控件失去焦点这一事件? 或者能不能在text失去焦点这个事件的处理过程中判断是哪一个控件将得到焦点?谢谢了!下面这个是我的text控件事情焦点的处理过程:
private void txtAlias_LostFocus(object sender, EventArgs e)
{
try
{
empName = ...;
if (empName != null && empName != "")
{
...
}
else
{
this.txtAlias.Text = "";
this.txtAlias.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
在一个Form上有很多控件,其中有一个是text控件,一个是button控件。 当焦点从该text控件离开时会做检查,如果检查失败,会再把焦点设置到该text控件上。
当点了button控件时,想直接关闭窗体。但是,如果焦点是在text上,我直接点button控件时该button控件就不起作用了。有没有办法判断我是在点了button控件后出发了text控件失去焦点这一事件? 或者能不能在text失去焦点这个事件的处理过程中判断是哪一个控件将得到焦点?谢谢了!下面这个是我的text控件事情焦点的处理过程:
private void txtAlias_LostFocus(object sender, EventArgs e)
{
try
{
empName = ...;
if (empName != null && empName != "")
{
...
}
else
{
this.txtAlias.Text = "";
this.txtAlias.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
然後加入:
foreach (Control ctr in this.Controls)
{
if (ctr.TabIndex == ((Control)sender).TabIndex + 1)
{
ctr.Focus();
break;
}
}
{
try
{
if (btnClose.Focused == true)//加上这个语句块就可以了
{
return;
}
empName = ...;
if (empName != null && empName != "")
{
...
}
else
{
this.txtAlias.Text = "";
this.txtAlias.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}