Form.ShowDialog() or Form.ShowDialog(this)?

One annoyance I found with ShowDialog() vs ShowDialog(this).

Run the TestApp, show the newform.ShowDialog(), click "show Desktop" on your taskbar or Quick launch toolbar, click on the TestApp on the taskbar. It shows the Mainform. You have to do an Alt-Tab to get to your newform.

VS

Run the TestApp, show the newform.ShowDialog(this), click "show Desktop" on your taskbar or Quick launch toolbar, click on the TestApp on the taskbar. It shows the newform on top.


Just to better understand the owner-owned relationship:

.NET allows a form to “own” other forms. Owned forms are useful for floating toolbox and command windows. One example of an owned form is the Find and Replace window in Microsoft Word. When an owner window is minimized, the owned forms are also minimized automatically. When an owned form overlaps its owner, it is always displayed on top.

(c) "Pro .NET 2.0 Windows Forms and Custom Controls" by Matthew MacDonald.


As ShowDialog shows the new form, an implicit relationship is established between the currently active form, known as the owner form, and the new form, known as the owned form. This relationship ensures that the owned form is the active form and is always shown on top of the owner form.

One feature of this relationship is that the owned form affects the behavior of its owner form (when using ShowDialog):

  • The owner form cannot be minimized, maximized, or even moved.
  • The owned form blocks mouse and keyboard input to the owner form.
  • The owner form is minimized when the owned form is.
  • Only the owned form can be closed.
  • If both owner and owned forms are minimized and if the user presses Alt+Tab to switch to the owned form, the owned form is activated.

Unlike the ShowDialog method, however, a call to the Show method does not establish an implicit owner-owned relationship. This means that either form can be the currently active form.

Without an implicit owner-owned relationship, owner and owned forms alike can be minimized, maximized, or moved. If the user closes any form other than the main form, the most recently active form is reactivated.

Although ShowDialog establishes an implicit owner-owned relationship, there is no built-in way for the owned form to call back to or query the form that opened it. In the modeless case, you can set the new form's Owner property to establish the owner-owned relationship. As a shortcut, you could pass the owner form as an argument to an overload of the Show method, which also takes an IWin32Window parameter (IWin32Window is implemented by Windows Forms UI objects that expose a Win32 HWND property via the IWin32Window.Handle property).

The behavior of forms in an explicit modal owner-owned form relationship is the same as its implicit modal counterpart, but the modeless owner-owned relationship provides additional behavior in the non-owner-owned modeless case. First, the modeless owned form always appears on top of the owner form, even though either can be active. This is useful when you need to keep a form, such as a floating tool window, on top of other forms within an application. Second, if the user presses Alt+Tab to switch from the owner, the owned forms follow suit. To ensure that the user knows which form is the main form, minimizing the owner hides the task bar buttons for all owned forms, leaving only the owner's task bar button visible.

(c) "Windows Forms 2.0 Programming" by Chris Sells, Michael Weinhardt.


"Currently active Window" usually refers to the foreground window, but only if it belongs to the current thread - see GetActiveWindow in MSDN.

(The actual information is in the community content, but the commenter is right that there is no "per-thread active window", AFAIK).

So when the user switched to another applications (or threads) window, you end up with some "default window". Even if .NET does some magic here, the modality will be broken: the intended parent window does not get disabled (e.g. you could switch to your main window, and close it, or modify something, which often breaks your application due to reentrancy).

Also, if another application is currently active, your dialog will not be shown on top, but it will be hidden behind some other window.

As a minor annoyance, the initial position is usually incorrect or misleading.

In practive, this happens rarely, though: if you open the dialog in response to a menu or button click on your main window, the user doesn't will virtually never manage to switch to another window.

However, it is technically possible, and quite likely to happen if you open the dialog in response to some automation, external message etc.

Tags:

C#

.Net

Winforms