窗口关闭时我的应用程序失去焦点
我有一个简单的两种表单,一种包含网格和按钮。当我单击按钮时,我的应用程序开始执行长时间操作。当它工作时,我显示另一个包含进度条的表单 我这样打开它:
_busyWindow.ShowDialog();
并定义
public partial class BusyWindow : DevExpress.XtraEditors.XtraForm
{
public BusyWindow()
{
InitializeComponent();
}
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
}
}
当操作完成时,我像这样隐藏表单
if (ended)
_busyWindow.Hide();
它工作正常。问题是,当我关闭第二个表单(相同的关闭代码)时,它也可以正常关闭,但我的主 GUI 失去了焦点。例如,如果我在应用程序后面打开了 Firefox,那么 Firefox 就会获得焦点。
仅当我在 busyWindow 打开时关闭第二个表单时才会发生这种情况,而在没有打开该表单时则不会发生这种情况(即,如果我打开该表单,我在不单击按钮的情况下关闭它,那么主 GUI 不会丢失重点)。
你知道发生了什么事或者我可以尝试在哪里搜索吗?
I have a simple two forms, one that contains a grid and a button. When I click the button, my application starts doing a long operation. While it is working, I show another form that contains a progress bar
I open it like this:
_busyWindow.ShowDialog();
And defined
public partial class BusyWindow : DevExpress.XtraEditors.XtraForm
{
public BusyWindow()
{
InitializeComponent();
}
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
}
}
When the operation is finished, I hide the form like this
if (ended)
_busyWindow.Hide();
It works fine. The problem is that when I close the second form (same closing code), it also closes fine but my main GUI loses the focus. For example, if I have the Firefox opened behind the application, then the Firefox gets the focus.
This only happens when I close the second form when the busyWindow has been opened, and no when it hasn't (ie, if I open the form, I close it without clicking on the button, then the main GUI doesn't lose the focus).
Do you know what is happening or where could I try to search?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在关闭子窗口之前设置其 Owner = null
Just set child's window Owner = null before closing it
可能有两种可能的解决方案使您能够将注意力集中在主窗口上:
//已编辑:下面示例中的主窗口将是带有网格和按钮的窗口。
由于您是通过
ShowDialog()
显示繁忙窗口,请尝试通过以下方式设置窗口的所有者:_busyWindow.ShowDialog(this);
。我早些时候遇到过类似的问题,这对我有用。由于您指定了 busyWindow 的所有者,因此当它关闭时,它会将焦点放回其所有者,即您的主窗口如果上述技术不起作用(它应该,因为它对我有用),您可以尝试将主窗口的引用传递给 busyWindow,然后在其关闭时设置主窗口的焦点。示例:
<代码>
_busyWindow.MyMainWindow = this; //MyMainWindow 引用您应用程序的 mainWindow
_busyWindow.ShowDialog();
以及 busyWindow 的 FormClosing 处的以下内容:
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
看看它是否有效。第一个解决方案应该有效。
希望有帮助。
谢谢&开窗快乐!
There could be two possible solutions to enable you to keep focus on your main window:
//Edited: Main Window in the below example would be the window with Grid and Button.
Since you are showing the busy window via
ShowDialog()
try setting the owner of the window by this:_busyWindow.ShowDialog(this);
. I had earlier faced a similar problem and this worked for me. Since you specify the owner of the busyWindow, when it closes it would put the focus back on its owner,i.e. your main windowIn case the above technique doesnt work (it should, as it worked for me), you could try to pass the reference of the main window to the busyWindow and then on its close set the focus of the main window. Sample:
_busyWindow.MyMainWindow = this; //MyMainWindow references mainWindow of your app
_busyWindow.ShowDialog();
And the following at the FormClosing of busyWindow:
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
See if it works. The first solution should work.
Hope it helps.
Thanks & Happy Windowing!