views:

1035

answers:

6

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.

+12  A: 

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.

Ed Swangren
You need to have a look at AutoCAD's dialogs. Very nicely designed IMHO.
Vulcan Eager
AutoCAD is pricey =)
Ed Swangren
+1  A: 

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.

Igor Brejc
I agree, can't stand 'em.
Ed Swangren
A: 

The modal/modeless paradym 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 paradym.

Blank Xavier
"Paradigm". Sorry, it was bugging me.
Ed Swangren
I'm *certain* I've seen "paradym"...? or perhaps it's just a popular mis-spelling?
Blank Xavier
+1  A: 

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.

__grover
"For modeless dialogs, Windows automatically disables the parent window"I don't think so. Neither is it true for modal dialogs. What Windows does is run a secondary message loop, preventing most messages other than WM_PAINT from reaching the dialog owner (owner != parent btw).
Vulcan Eager
Ok, modeless was a typo. But it is true that the parents of modal dialogs are disabled. You can check this for yourself using Spy++. If done enough tinkering with them to be certain of that. The additional message loop does not prevent the parent from receiving messages interacting with the user.
__grover
+2  A: 

Just close the modal dialog. It doesn't get disposed like normal Form instances do so you simply bring it back alive by calling ShowDialog() again. Note that calling Hide() on a modal dialog also closes it.

Hans Passant
A: 

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.

Lukáš Rubeš