views:

583

answers:

3

I have some code, which calls the form.ShowDialog(IWin32Window) overload rather than the form.ShowDialog() one, but which passes in the current active window as the parameter.

Using the other overload seems to leave the behaviour intact. Is it safe to switch to the simpler overload? When would I want to pass in the IWin32Window parameter?

A: 

You can use the IWin32Window parameter if you want to parent your form in another application or form.

So for example you can get the handle of another application or form and show your form as a part of that application. You can find more information here

Nathan W
A: 

You should use the void constructor under normal circumstances. Like Nathan said, only use the IWin32Window constructor if you want to show the dialog with a specific owner. Which is not typically necessary.

The documentation for ShowDialog() states, "Shows the form as a modal dialog box with the currently active window set as its owner." So, unless you explicitly need to set the owner of a form to a window that is NOT the active window, there is no need to use ShowDialog(IWin32Window).

NascarEd
It's always a good idea to write your code to convey your intent, so even if you only want to parent it to the active window, specifying that window as a parameter makes your code explicit in its intentions rather than relying on those who maintain your code to determine which window was intended to be active when the dialog is shown.
Jeff Yates
So do you think that you should only use the parameterless overload when you want *whichever* window is the active window to be the parent?
Ant
+2  A: 

The IWin32Window parameter is for specifying the owner of the dialog. In this case it's an interface so that you can have non-managed windows (those from a COM object you're using, for example) as the dialog's owner.

I have to disagree with the users here who say that you shouldn't specify an owner. In fact, it's always advisable to specify an owner for a form when possible. While dialogs may not be as important as non-modal forms, getting into the habit of specifying an owner is always a good idea.

Adam Robinson
It's good practise because it clearly shows intent.
Jeff Yates