Click here to Skip to main content
14,550,734 members

TopMost MessageBox

Rate this:
4.53 (32 votes)
Please Sign up or sign in to vote.
4.53 (32 votes)
1 May 2007
A simple wrapper class to ensure MessageBox messages are visible.

Screenshot - TopMostMessageBox.jpg



I was writing some custom installer code for a Visual Studio installer project and had to display messages to the user in my custom installer class. The problem was that when using the standard System.Windows.Forms.MessageBox.Show method the message box was appearing behind the main installer window. There are no options on the MessageBox class to alter this behavior. I could just see the support lines lighting up with calls of the install hanging.


The solution was to make the window owning the MessageBox a TopMost window. One difficulty with that was that the custom installer class does not have a reference to the main installer window and has no UI of its own. So, a new form had to be created within my custom installer class which could be the owner of the MessageBox. Setting this form as a TopMost window causes the MessageBox to be a TopMost window as well. Thus, the messages displayed by my custom installer would always be visible. Special actions are taken to ensure this new form is not visible since it would merely be a distraction to the user.

This solution was packaged into a wrapper class called TopMostMessageBox.

Using the code

Here is how you could use the TopMostMessageBox in your code.

    "This will appear in a message box that is a topmost window",
    "Title", MessageBoxButtons.AbortRetryIgnore);

Here is the declaration of the TopMostMessageBox class.

static public class TopMostMessageBox
    static public DialogResult Show(string message)
        return Show(message, string.Empty, MessageBoxButtons.OK);

    static public DialogResult Show(string message, string title)
        return Show(message, title, MessageBoxButtons.OK);

    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);
        // Make this form the active form and make it TopMost
        topmostForm.TopMost = true;
        // Finally show the MessageBox with the form just created as its owner
        DialogResult result = MessageBox.Show(topmostForm, message, title, 
        topmostForm.Dispose(); // clean it up all the way

        return result;

Points of Interest

  • I tried to make this wrapper mimic the existing MessageBox.Show method, but many overrides of this method are not included in this simple wrapper. However, they can easily be added if you need them.
  • The hidden form is created off the visible screen to prevent any flickering. This is done by finding the size of the virtual desktop and positioning the hidden form just off past the right side of it. I do not use the physical screen size to account for multiple monitors.
  • The combination of Focus(), BringToFront(), and TopMost seemed to be needed to get the MessageBox to show up properly and have input focus.
  • To clean up the hidden form completely I call Dispose on the form.

Other Options

I could have used the MessageBoxIndirect function to accomplish the same thing. However, this is a bit simpler as it does not involve reproducing any Win32 structures.


1.0 - 21 Apr 07 - First Posting


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


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.

Comments and Discussions

PraiseThanks A lot. Pin
CringTee28-Oct-16 5:11
MemberCringTee28-Oct-16 5:11 
GeneralMy vote of 5 Pin
rlovelacecap26-Sep-15 7:26
professionalrlovelacecap26-Sep-15 7:26 
QuestionForm Visibility Pin
Dietmar Kern31-Aug-15 5:19
MemberDietmar Kern31-Aug-15 5:19 
atogwe24-Mar-15 10:30
Memberatogwe24-Mar-15 10:30 
QuestionIs there simpler solution? Pin
Stan Huang3-Feb-15 19:12
professionalStan Huang3-Feb-15 19:12 
QuestionResult is very machine to machine Pin
shain198914-Jan-15 1:47
Membershain198914-Jan-15 1:47 
QuestionSimpler way - Using MessageBoxOptions-Enumeration Pin
zaengi12-Aug-13 5:01
Memberzaengi12-Aug-13 5:01 
SuggestionReally good, but... Pin
Wrightboy23-Apr-13 7:13
MemberWrightboy23-Apr-13 7:13 
BugRe: Really good, but... Pin
trabezan22-Oct-14 2:50
Membertrabezan22-Oct-14 2:50 
GeneralMy vote of 5 Pin
Akram El Assas26-Oct-12 0:26
MemberAkram El Assas26-Oct-12 0:26 
GeneralMy vote of 5 Pin
gurunio5-Sep-12 20:31
Membergurunio5-Sep-12 20:31 
GeneralThanks! Pin
Luke the HB20-May-12 23:34
MemberLuke the HB20-May-12 23:34 
GeneralMy vote of 5 Pin
CDFaux4-Apr-12 8:46
MemberCDFaux4-Apr-12 8:46 
GeneralMy vote of 5 Pin
Mdawasd19-Nov-11 3:55
MemberMdawasd19-Nov-11 3:55 
QuestionAwesome stuff! Pin
Member 786274820-Jul-11 15:55
MemberMember 786274820-Jul-11 15:55 
GeneralExcellent Pin
Manjit Dosanjh16-May-11 23:40
MemberManjit Dosanjh16-May-11 23:40 
GeneralMy vote of 5 Pin
Manjit Dosanjh16-May-11 23:38
MemberManjit Dosanjh16-May-11 23:38 
GeneralMy vote of 5 Pin
hsmcc13-Mar-11 4:57
Memberhsmcc13-Mar-11 4:57 
GeneralAwesome! Pin
Felix De Herrera11-May-10 7:37
MemberFelix De Herrera11-May-10 7:37 
gvanto29-Nov-09 14:51
Membergvanto29-Nov-09 14:51 
GeneralGood work, there is only one detail ... Pin
Isidora7915-Oct-09 4:44
MemberIsidora7915-Oct-09 4:44 
GeneralMy vote of 2 Pin
Andy Missico29-Jan-09 10:16
MemberAndy Missico29-Jan-09 10:16 
AnswerUse Microsoft.VisualBasic.Interaction.MsgBox Pin
Andy Missico29-Jan-09 10:14
MemberAndy Missico29-Jan-09 10:14 
QuestionAny way of determining which screen to show the MessageBox on? Pin
Ken Lyon1-Dec-08 1:16
MemberKen Lyon1-Dec-08 1:16 
GeneralNice workaround - a wee observation, though Pin
Ken Lyon1-Dec-08 1:07
MemberKen Lyon1-Dec-08 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 1 May 2007


39 bookmarked