Both ShowDialog();
and ShowDialog(IWin32Window);
seem to do the exact same thing to me. The documentation isn't very clear either.
I've been told that ShowDialog(IWin32Window);
will ensure that the dialog window is on top of the whatever is passed in as the owner window.
The MSDN documenation makes no mention of this, so it feels like a bit of black magic.
http://msdn.microsoft.com/en-us/library/w61zzfwe.aspx makes it pretty clear that the parameter represents the owner window. And that is the behavior of an owner window - it stays behind its children and doesn't close leaving its children still displayed.
When ShowDialog() is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method (ShowDialog(IWin32Window)).
This is stated in the MSDN documenation.
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
Internally ShowDialog()
calls ShowDialog(IWin32Window)
with a null argument. If the argument is not null, it is set as the form's owner. If owner is null, it will use GetActiveWindow()
to get a IntPtr
to the active window and use this as the owner. You can check it out yourself using ILSpy.
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With