Temporarily Hide a Modal Dialog

2019-08-17 02:13发布

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.

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-17 03:02

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.

查看更多
做自己的国王
3楼-- · 2019-08-17 03:03

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楼-- · 2019-08-17 03:05

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.

查看更多
我只想做你的唯一
5楼-- · 2019-08-17 03:06

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楼-- · 2019-08-17 03:12

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().

查看更多
对你真心纯属浪费
7楼-- · 2019-08-17 03:14

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.

查看更多
登录 后发表回答