Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

TopMost MessageBox

, 1 May 2007
A simple wrapper class to ensure MessageBox messages are visible.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace TopMostMessageBox
{
    /// <summary>
    /// Displays MessageBox messages as a top most window
    /// </summary>
    static public class TopMostMessageBox
    {
        /// <summary>
        /// Displays a <see cref="MessageBox"/> but as a TopMost window.
        /// </summary>
        /// <param name="message">The text to appear in the message box.</param>
        /// <param name="title">The title of the message box.</param>
        /// <returns>The button pressed.</returns>
        /// <remarks>This will display with no title and only the OK button.</remarks>
        static public DialogResult Show(string message)
        {
            return Show(message, string.Empty, MessageBoxButtons.OK);
        }

        /// <summary>
        /// Displays a <see cref="MessageBox"/> but as a TopMost window.
        /// </summary>
        /// <param name="message">The text to appear in the message box.</param>
        /// <param name="title">The title of the message box.</param>
        /// <returns>The button pressed.</returns>
        /// <remarks>This will display with only the OK button.</remarks>
        static public DialogResult Show(string message, string title)
        {
            return Show(message, title, MessageBoxButtons.OK);
        }

        /// <summary>
        /// Displays a <see cref="MessageBox"/> but as a TopMost window.
        /// </summary>
        /// <param name="message">The text to appear in the message box.</param>
        /// <param name="title">The title of the message box.</param>
        /// <param name="buttons">The buttons to display in the message box.</param>
        /// <returns>The button pressed.</returns>
        static public DialogResult Show(string message, string title, MessageBoxButtons buttons)
        {
            // Create a host form that is a TopMost window which will be the parent of the MessageBox.
            Form topmostForm = new Form();
            // We do not want anyone to see this window so position it off the visible screen and make it as small as possible
            topmostForm.Size = new System.Drawing.Size(1, 1);
            topmostForm.StartPosition = FormStartPosition.Manual;
            System.Drawing.Rectangle rect = SystemInformation.VirtualScreen;
            topmostForm.Location = new System.Drawing.Point(rect.Bottom + 10, rect.Right + 10);
            topmostForm.Show();
            // Make this form the active form and make it TopMost
            topmostForm.Focus();
            topmostForm.BringToFront();
            topmostForm.TopMost = true;
            // Finally show the MessageBox with the form just created as its owner
            DialogResult result = MessageBox.Show(topmostForm, message, title, buttons);
            topmostForm.Dispose(); // clean it up all the way

            return result;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Vance Kessler
Web Developer
United States United States
I have been developing .NET applications since 2001 and have been working in software development since 1989.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 May 2007
Article Copyright 2007 by Vance Kessler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid