Search code examples
c#messagebox

Custom MessageBox For Errors and Information


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;

        }
    }
}

Solution

  • 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.