Search code examples
c#modal-dialogmdishowdialogmdichild

Wait for MDI child to close, similar to ShowDialog()


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?


Solution

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