C# / .NET messagebox is not modal

Why is a C#/.NET message box not modal?

Accidentally, if the message box goes behind our main UI, then the main UI doesn't respond, until we click OK (on our message box).

Is there a workaround other than creating a custom message box?


You need to assign the MessageBox owner property to the main UI window (look at the 3rd constructor).


This is a simple C# new Windows-Forms application:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message = "You did not enter a server name. Cancel this operation?";
            string caption = "No Server Name Specified";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            // Displays the MessageBox.
            result = MessageBox.Show(this, message, caption, buttons);
            if (result == DialogResult.Yes)
            {
                // Closes the parent form.
                this.Close();
            }
        }
    }
}

As Dusty states in his answer, a message box is a modal dialog. Specify the owner property. In this example, the owner is denoted by the keyword this.


To get system modal messagebox set MessageBoxOptions.DefaultDesktopOnly.


A modal pop-up is technically defined as a pop-up box that interrupts the normal flow of the application...not necessarily one that stays on the top of all other windows so the behavior you're describing is correct for a modal popup.

Modal Window

Here's a project on CodeProject that tries to mimic the "always on top" functionality for a MessageBox style Modal window:

CodeProject: TopMost MessageBox