tags:

views:

878

answers:

3
+3  Q: 

MessageBox.Show()

I would like to Show my Messagebox in the center of its parent form. if i move the form and show the messagebox, it always show in the center of the desktop. i want it to appear along with the form. Can you give me some tricks and advice?

+1  A: 

Set the owner of the message box window to your window (using the first parameter of .Show()), instead of not setting an owner.

See here for a reference.

Greg Hewgill
can you give me some example?i've tried this:IWin32Window win = this; MessageBox.Show(win,"TESTING");Although, same result. Messagebox still pop up in the middle of the desktop
Jepe d Hepe
I suppose that depends on what `this` is. I'm afraid that my experience is limited to the native Win32 `MessageBox()` function, which is a bit different, and I got halfway through answering before I realised you were using WinForms. So I modified my answer to fit what I found in the reference, but I may still have some details missing. :)
Greg Hewgill
Passing a window handle to MessageBox does not display the message box in the center of the parent. It is just to enable minimize/maximize with the parent and there is no specific task icon for the message box window.
Yogesh
can you give me an example to display messagebox in the center of the parent?
Jepe d Hepe
@Yogesh: interesting, perhaps the WinForms MessageBox isn't implemented using the Win32 MessageBox function.
Greg Hewgill
+1  A: 

I have done this before in C#. Here's what I remember.

Define a class:

public class IWindowWrapper : System.Windows.Forms.IWin32Window
{
    public IWindowWrapper(IntPtr handle)
    {
        this.Handle= handle;
    }

    public IntPtr Handle
    {
        get; 
        set;
    }
}

Define a class based on MessageBox. Create a class based on MessageBox and create a new Show method :

public string Show(IWin32Window owner)
{
   if(owner == null)
   {
    this.ShowDialog();
   }
   else
   {
      //parentWindow.
      this.StartPosition = FormStartPosition.CenterParent;
      this.ShowDialog(owner);
   }

}

In your calling code (here it is assumed to be a winform and msgBox is based on the new message box class) call the new Show method and pass an IWindowWrapper instance on Show e.g.

msgBox.Show(new IWindowWrapper(this.Handle))
ChrisBD
When you say "based", do you mean inherit? Im kinda new in C#. :)
Jepe d Hepe
Yes e.g. public class MyMessageBox : MessageBox ... etc.
ChrisBD
You don't have to use C# to do this, but that's what I used. Is that the language that you're programming in?
ChrisBD
The Second Code. Am i going to Inherit a Messagebox? or just create a form with the same style as Messagebox? this.ShowDialog() is not availabe if i inherit a messagebox
Jepe d Hepe
I'm using C#. i think this is not possible for me. anyone?
Jepe d Hepe
@ChrisBD: How do you plan to inherit a MessageBox? Can you show an example?
Yogesh
Give me a few minutes whilst I type out some code.
ChrisBD
+1  A: 

The best way to do this is to use Window Hooks and center the message box yourself. There is a perfect article which shows this usage.

You can find it here: http://www.codeproject.com/KB/dialog/CenterDialog.aspx

You can also use the class in your application without diving in too deep to find out how it actually works.

Yogesh
Article is great. Just need to understand it before applying it to my app. tnx. is there much easier way? heh..
Jepe d Hepe
The easiest way would be make a new MessageBox form yourself. That will be according to your taste and will look like the way you want it to be. Also, you can add any functionality you like.
Yogesh