Question

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

I heard that if I call form.ShowDialog() without specifying the owner, then there can be a case when I will not see the dialog form on screen (it will be hidden with other windows). Is it true? I used ShowDialog() without specifying the owner hundreds of times and I never had any problems with that.

Can you please explain in which situation I could get the described problem?

UPDATE:

Well, I did many experiments and I couldn't get any real unexpected problems with using ShowDialog() (without specifying the owner).

So I think it's just rumors that ShowDialog() can lead to problems. If you don't agree - give me a code sample please that leads to a problem.

 45  64289  45
1 Jan 1970

Solution

 26

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.

2010-01-11

Solution

 17

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.

2013-06-30