Search code examples
c#winformsshowmodaldialog

MessageBox.Show() Not showing on top of ShowDialog form


I have the following code which displays a form as ShowDialog().

static void Main()
{

    DialogResult oDialogResult = oLogin.ShowDialog();

    if (oDialogResult == DialogResult.OK)
    {
       try
       {
           //do something here
       }
       catch (Exception Ex)
       {
            MessageBox.Show(Ex.Message, "IMPORTANT MESSAGE", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
   }
}

The problem is the call MessageBox.Show is not showing the message box in front of (on top of) the already showing oLogin.ShowDialog() form. Do you know why ?

I tried changing the MessageBox.Show to include the owner form as is the following;

MessageBox.Show(oLogin, Ex.Message, "IMPORTANT MESSAGE", MessageBoxButtons.OK, MessageBoxIcon.Error);

But still have the same problem.


Solution

  • Change the code in your Main() method to what the visual studio project template originally created:-

    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(oLogin);
    

    Then, add your try/catch block to a FormClosing event handler on whatever class is oLogin