tags:

views:

1975

answers:

6

MessageBox.Show has forms like MessageBox.Show( ownerWindow, .... ).

What do I gain by assigning a owner window?

A: 

if i'm not wrong this prevents to owner window to Focus() until the messagebox is closed.

Joachim Kerschbaumer
Can anyone cite an example of a way the owner window can recieve the Focus() while a Message Box is open?
jyoung
No. Message boxes are always modal so you cannot do anything with the underlying windows.
BlackWasp
+2  A: 

Setting the owner causes the owner to be disabled while the message box is open.

If you don't set the owner then the user can click something else or even close the owner while the message box is open, then when the message box closes and the code after the call to MessageBox.Show runs your program can be in an unknown state - or if the owner was closed you are now running code inside a window that no longer exists and any calls to methods of WinForms or WPF (or for that matter also WinAPI and any other framework) are likely to cause a crash.

Nir
Can anyone cite an example of a way the owner window can "the user can click something else or even close the owner " while a Message Box is open?
jyoung
Should read: Can anyone cite an example of a way "the user can click something else or even close the owner " while a Message Box is open?
jyoung
Actually, you can't close the owner if you don't specify the owner because then there is no owner - but the user can close any application window then. However, if I remember correctly, MessageBox.Show chooses the foreground window as owner if you don't specify one.
OregonGhost
+7  A: 

A message box is a modal form, which means that its parent window is disabled until the message box is dismissed.

If a Show() overload is called that does not take an owner identifier, then a parent form is usually chosen automatically. I can't find anything in the documentation that describes how that form is chosen, but my experience is that if the message box is displayed inside the GUI thread (i.e., the main thread, or message pump thread), then the active window for that thread is chosen as the parent.

Other threads may create message boxes with no parent form. This could be a bad situation, because it could sit behind the active window, and the user will not even know it is there until they close the program. To avoid this, you could pass the handle of the application's main window to the Show method, which will disable that window for the duration of the message box.


ADDED: I've been thinking about this and now I'm not so sure. The snippet from reflector that you gave below makes me think that maybe the thread doesn't matter. (I did say that I couldn't find anything in the documentation!)

I'd have to go back and look at the code to make sure, but I think the message boxes that I used to lose behind the main form may have actually been custom message box forms, in which case my experience is faulty, and you never ever have to supply a parent form parameter.

Sorry for the confusion.

My recommendation now is to never supply this parameter unless you need something other than the active window to be the main form.

Jeffrey L Whitledge
Thanks. So it looks like you only need the use the window owner parameter if you run Messagebox.Show from a different thread from your GUI.
jyoung
A: 

Using Net Reflector, I just found this code in Messagebox.Show:

else if (owner == IntPtr.Zero)
    owner = UnsafeNativeMethods.GetActiveWindow();

So if you do not nested ownership ( window--(owns)-->window--(owns)->messageBox), leaving out the ownerWindow sets the owner you would normally choose.

jyoung
Well, no, that's not true; you've discovered what others have already said, which is that the method will assign the owner to be the active window. If you want to be absolutely clear about which window is blocked while the message is active, you should specify it in the method call.
Rob
Unless you want another window to be the owner. But you're right, I never set the owner for MessageBox.Show in SWF applications and never saw unexpected behaviour.
OregonGhost
Unless if there is a benefit, "being absolute clear", is a bad thing.It just makes thing slower to read, more error prone, and less maintainable.So as a good programmer, I always look for the benifit.
jyoung
I think that there is valid difference between being "absolutely clear" and being "verbose". The former is a *must* to write maintainable code the later one may result in clutter.For this particular case: do you expect your colleges to use reflector to see what is happening?
Christian.K
A: 

It turns out that window ownership is not transitive. In the case where you have form, spawning a form, spawning a MessageBox, The MessageBox.Show needs to use the ownerWindow parameter. The original form should be set as the owner window of the MessageBox.

jyoung
+1  A: 

The documentation seems to imply that the only purpose of the owner parameter is if you specify MB_HELP, the message box knows which window it should send the WM_HELP message to.

http://msdn.microsoft.com/en-us/library/ms645505%28VS.85%29.aspx


Oh, just realised the OP was about .net - I gave an answer about winapi - sorry!

Tim Gradwell