0

This is the context:

//Form
    //Popup modal Form
        //Messagebox

The MessageBox code:

DialogResult oResult = AppMessageBox.Show(this, "Replace the existing codes?",
        MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

The AppMessageBox class is nothing special:

public class AppMessageBox
{
    private static String _strName;
    private static String strName
    {
        get
        {
            if (_strName == null)
            {
                _strName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            }
            return _strName;
        }
    }

    public static DialogResult Show(String strMessage, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information)
    {
        return MessageBox.Show(strMessage, strName, buttons, icon);
    }
    public static DialogResult Show(IWin32Window hWndOwner, String strMessage, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information)
    {
        return MessageBox.Show(hWndOwner, strMessage, strName, buttons, icon);
    }

}

The code displaying the message box is called from the form's button handler. Yet, it is displaying central to the screen and not central to the inner form. Why?

I thought that if I passed "this" it would center it to the current form.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • I found this: http://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform Seems that I have no control over where it gets positioned without doing a lot of work. Don't understand why it does not work anyway. Consistency with MFC etc.. – Andrew Truckle Jun 01 '16 at 15:40
  • 1
    Yes, one would think that setting the parent window would be all that is needed, but the MessageBox API doesn't work that way. It *always* centers itself in the middle of the screen, probably for some backwards compatibility reasons. Hans's code will work well, and you can add it to your project once and then promptly forget it—the benefits of object-orientation. – Cody Gray - on strike Jun 01 '16 at 15:59

0 Answers0