tags:

views:

2873

answers:

7

I am trying to get center of parent form, not center of screen behavior. Passing in the parent form seems to only control the ownership of the window. These classes are sealed, so I do not see how I can do any WinProc tricks. Rewriting the classes is not an appealing option. Any other ideas?

A: 

I do not know much about the dialogs or message boxes but, you can position a new Form as you are looking for with its StartPosition property.

System.Windows.Forms.Form f = new Form();
f.StartPosition = FormStartPosition.CenterParent;
tafa
Trouble is, he doesn't have a new form. MessageBox uses a static Show() method.
itsmatt
+1  A: 

Without digging into some ugly P/invoke code to find and move the window after it is displayed, this simply impossible. If you pursue this, the message box will "jump" to the new position, which is generally worse than not having it centered. If the position is truly that important, you are much better off creating your own message box, or adopting one from CodeProject.

HTH

Nescio
+2  A: 

As HTH explained above, there are ugly ways to do it. Here is a simple class that will wrap the dialog box and center it in the parent application/Form.

It takes the ugly ways and wraps them to something slightly more simple.

While I haven't implemented this or really delved into the source, it's a good place to start. CodeProject CenterDialog

Hope this helps.

Paige Watson
This also works.
jyoung
A: 

You could use the VB.NET Interaction.InputBox method. If you're working in C#, just add a reference to Microsoft.VisualBasic, then call it. This method lets you specify X and Y coordinates. You'd have to settle for a dialog that has an input box, but that might be preferable to rolling your own.

I think that's your only other option -- like you said, MessageBox, etc. are sealed classes, so you'd have to back up to CommonDialog and derive your own (position-specifiable) class from that. Granted, it shouldn't be hard, but I can understand if you don't want to have to write/maintain it.

Coderer
A: 

you are much better off creating your own message box

OT, but this is one of my pet peeves. The Office team did this, and their MessageBox is missing a capability of the standard MessageBox - the ability to copy the MessageBox as text to the clipboard using Ctrl-C (useful for bug reports etc).

Joe
+1  A: 

After some digging I found http://support.microsoft.com/kb/180936. This works.

jyoung
A: 

Extended MessageBox .NET Library (assembly)

Among other useful features, includes two positioning modes: by absolute coordinates, by aligning to specified border of active monitor.

This is not a replacement for MessageBox.Show, but an extension to it. Other features: message font and font color selection, adjustable background color, user-defined button captions and button fonts, capturing text input from user and more.

Anatoliy Mogylevets