等待MDI子进程关闭,类似于ShowDialog()
我有一个 MDI 应用程序,我想在其中使用模式对话框...是的,我知道这有点违反 MDI 的原则...无论如何,我的主窗口更像是一个“工作区”然后还有其他的事情。
回到主题,如何等待 MDI-Child 关闭?一些示例代码:
public void DoSomething() {
String searchterm = this.TextBox1.Text;
MyItem result = MySearchForm.GetItem(searchterm);
if(MyItem != MyItem.Empty) {
// do something
}
}
MySearchForm 是主窗口的 MDI 子级,因此我无法使用 ShowDialog(),但我仍然想使用阻塞方法来等待窗口关闭并返回结果。我想过在另一个线程上调用它并等待该线程退出,但这也不适用于 MDI。
有人有想法吗?
I've got a MDI-Application in which I'd like to use modal dialogs...yes, I know that this is a little against the principles if MDI...anyway, my main window is more of a 'workspace' then anything else.
Back to topic, how can I wait for an MDI-Child to close? Some example code:
public void DoSomething() {
String searchterm = this.TextBox1.Text;
MyItem result = MySearchForm.GetItem(searchterm);
if(MyItem != MyItem.Empty) {
// do something
}
}
MySearchForm is a MDI-Child of the main window, so I can't use ShowDialog(), but I'd still like to use a blocking method to wait for the window to close and return the result. I thought about calling it on another thread and wait for that one to exit, but that also doesn't work with MDI.
Does somebody have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 MDI 应用程序中使用对话框是很正常的,它并不违反 MDI 约定。只是不要将其设为 MDI 子窗口。这很糟糕,因为你无法使其成为模态。如果您将其设置为非模态,那么当用户最小化窗口时会发生令人困惑的事情。
只需使用 ShowDialog(owner) 或 Show(owner) 方法(分别为模式和非模式)并将 MDI 父级作为所有者传递。该对话框将始终位于子窗口的顶部。您通常确实需要 StartPosition = Manual 并设置 Location,以便可以确保它在父框架内的适当位置启动。
Using dialogs in an MDI application is quite normal, it doesn't violate MDI conventions. Just don't make it a MDI child window. That's bad because you cannot make it modal. And if you make it non-modal then confuzzling things happen when the user minimizes the window.
Just use the ShowDialog(owner) or Show(owner) method (respectively modal and non-modal) and pass the MDI parent as the owner. The dialog will always be on top of the child windows. You typically do want StartPosition = Manual and set Location so you can be sure it starts up at an appropriate position within the parent frame.
尝试禁用主窗体,然后在子窗体关闭时重新启用它。有点像这样:
Try disabling the main form, then re-enabling it when the child form closes. It would be a bit like this:
重载主窗口的FormClosing事件:
Overload the FormClosing event of the main window:
如果 MDI 子项丢失,只需将焦点移回该子项即可。在 MDI 子窗口中挂钩 LostFocus 事件并使用 this.SetFocus();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter.aspx
Simply move focus back to the MDI child if it loses it. Hook the LostFocus event in the MDI child window and use this.SetFocus();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter.aspx