Temporarily Hide a Modal Dialog

2019-08-17 02:23发布

问题:

I have a modal dialog displayed with the main application window set as owner (as in dialog.ShowDialog(mainAppWindow)) and I would like to temporarily allow the user to interact with the main application window before returning to the modal dialog again.

How can I do this? I am using Windows Forms.

Edit: AutoCAD does this well.

回答1:

Just close the modal dialog. It doesn't get disposed like normal Form instances do so you simply bring it back alive by setting its DialogResult property back to None and calling ShowDialog() again.

Note that calling Hide() on a modal dialog also closes it, necessarily so since all of your app's windows are disabled. No different from Close().



回答2:

Then I do not think that you want a modal dialog...

The whole purpose of a modal dialog is that the user cannot do anything until they have gotten rid of it in some way. I think that you should just create your own form class to act the way that you would like.



回答3:

Take a look at http://en.wikipedia.org/wiki/Modal_window#Criticisms... There's a school of thought that you shouldn't use modal windows in the first place.



回答4:

You need to enable the parent window again. For modal dialogs, Windows automatically disables the parent window and reenables it if the modal dialog was closed.

I haven't tried, but it should be sufficient to set the Enabled property of your parent form to true. If that doesn't work using the EnableWindow Win32 API does work.



回答5:

For some reason I have to face same problem in .NET. I have (main) form showing modal dialog, which I need to hide, interact with main window, and return to the modal dialog again.

I personally do not understand consequences from the Windows (API) point of view, but following solution works for me.

Whole trick lies in setting main form to disabled before showing modal dialog (when main form is not set to Enabled = false explicitly, then after hiding modal dialog no interaction can be done with it even when Enabled = true is called).

As response to modal dialog event (called NeedInteraction), I just hide modal dialog, enable main form, in some loop do interaction with user, disable main dialog and show modal dialog again.

void ShowDialog()
{
  var dialog = new MyModalForm();
  dialog.NeedInteraction += (sender, eventArgs) =>
  {
    dialog.Hide();
    Enabled = true;

    //wait till user finishes working with main window

    Enabled = false;
    dialog.Show();
  }

  Enabled = false;
  dialog.ShowDialog();
  Enabled = true; //don't forget to make it enabled afterwards
}

It might not be clean solution (as is not need for hiding modal dialog), but it works at least for my situation.



回答6:

The modal/modeless paradigm is that if you want the user to be able to interact with the main application, use a modeless window and if you don't, use a modal. If you want to stop him using the main application - but then use it - but then not use it - your user interface design does not work with the modal/modeless paradigm.