等待MDI子进程关闭,类似于ShowDialog()

发布于 2024-09-07 11:07:16 字数 480 浏览 10 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亽野灬性zι浪 2024-09-14 11:07:16

在 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.

孤云独去闲 2024-09-14 11:07:16

尝试禁用主窗体,然后在子窗体关闭时重新启用它。有点像这样:

  public void DoSomething() 
  {
    searchForm.Show();
    searchForm.SearchTerm = this.TextBox1.Text;
    searchForm.FormClosing += new FormClosingEventHandler(searchForm_FormClosing);

    this.Enabled = false
  }

  void searchForm_FormClosing(object sender, FormClosingEventArgs e)
  {
    this.Enabled = true;

    // Get result from search form here
    MyItem result = searchForm.GetItem();
    if(MyItem != MyItem.Empty) // do something
  }

Try disabling the main form, then re-enabling it when the child form closes. It would be a bit like this:

  public void DoSomething() 
  {
    searchForm.Show();
    searchForm.SearchTerm = this.TextBox1.Text;
    searchForm.FormClosing += new FormClosingEventHandler(searchForm_FormClosing);

    this.Enabled = false
  }

  void searchForm_FormClosing(object sender, FormClosingEventArgs e)
  {
    this.Enabled = true;

    // Get result from search form here
    MyItem result = searchForm.GetItem();
    if(MyItem != MyItem.Empty) // do something
  }
眼前雾蒙蒙 2024-09-14 11:07:16

重载主窗口的FormClosing事件:

void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        // User clicked the close button.
        // Cancel if dialogs are open.
        if (dialogsOpen)
        {
            e.Cancel = true;
        }
    }
}

Overload the FormClosing event of the main window:

void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        // User clicked the close button.
        // Cancel if dialogs are open.
        if (dialogsOpen)
        {
            e.Cancel = true;
        }
    }
}
为人所爱 2024-09-14 11:07:16

如果 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文