1

Based on my research, the only way to get a MessageBox Form to Center on the parent form is to write a custom MessageBox class. I have successfully implemented the CustomMessageBox Form and am able to center my errors and informational messages on the parent form. However, I cannot figure out how to make the CustomMessageBox form static so that I do not have to instantiate my new CustomMessageBox Form. I want to be able to just call a static method like below:

CustomMessageBox.Show(type, message, etc...)

The basic version of my MessageBox class is below. Ideally, I would like to have the functionality to display this form without having to instantiate my CustomMessageForm. Is this possible?

namespace MarineService
{
    public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
    {
        private static CustomMessageForm form = new CustomMessageForm();

        public CustomMessageForm()
        {
            InitializeComponent();
        }

        public void ShowDialog(string type, string message)
        {
            this.Text = type + "Information";
            this.groupMessage.Text = type + "Information";
            this.memoEditMessage.Lines[0] = message;

        }
    }
}
Grasshopper
  • 4,717
  • 9
  • 36
  • 62
  • One idea that came to mind while I was working on the CustomMessageBox was to create a static utility class that has a static method on it which creates a new instance of my CustomMessageBox and then displays it. However, if this solution is implemented I would have to pass the parent form into the static method on the utility class to center the new CustomMessageBox on the parent window. I'm not sure if this is good design since I am new to windows programming. Any comments and/or feedback are welcome. – Grasshopper Jan 15 '12 at 13:27
  • 2
    possible duplicate of [Winforms-How can I make MessageBox appear centered on MainForm?](http://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform) – Hans Passant Jan 15 '12 at 13:29

1 Answers1

2

Something like this:

public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
{
        private static CustomMessageForm form = new CustomMessageForm();

        public CustomMessageForm()
        {
            InitializeComponent();
        }

        private void ShowDialog(string type, string message)
        {
            form .Text = type + "Information";
            form .groupMessage.Text = type + "Information";
            form .memoEditMessage.Lines[0] = message;
            form.ShowDialog();

        }

        public static Show(string type, string message)
        {
           if(form.Visible)
              form.Close();
           ShowDialog(type, message);
        }
}

And use this like:

CustomMessageForm.Show("customtype", "warning!");

Something like that, just an idea.

If this is not what you're asking for, please clarify.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thanks, this is exactly what I needed. I read Hans post but, it did not click that I could just make the Show method static without making the entire class static until I read your post. Appreciate the help. – Grasshopper Jan 15 '12 at 13:40