Displaying a MessageBox on top of all forms, setting location and/or color

34,618

Solution 1

1) The MessageBox.Show method has an overload that takes a first parameter of Window type. If you use that overload instead of just Show(string), ie.:

class MyForm : Form {
    void method(){
       MessageBox.Show(this, "blablablablabla");
    }
}

then the MessageBox will show up in a 'modal' mode and it will be exactly on top on that form. Now just ensure that you pass that topmost form and you're done. Side effect is that the 'modal' mode will cause the Messagebox to BLOCK the original window until the message is dismissed.

2) No, that is not possible directly. However, you can play hard with .Net and get a "handle" to the messagebox and then move the window via P/Invoke to some WinApi functions, but I recommend you not.

3) No, that's just not possible with MessageBoxes

What you want to achieve in (2) and (3) is not possible, because the MsgBox is meant to be simple. To get that things you will have to write your own tiny form that will act as a message box, and present that form instead of the message box. That form will be able to have any styling, any position and any behaviour you like.

Solution 2

I use this.

MessageBox.Show(
                "message",
                "title",
                MessageBoxButtons.OK,
                messageBoxIcon,
                MessageBoxDefaultButton.Button1,
                (MessageBoxOptions)0x40000); // this set TopMost

Solution 3

To show a MessageBox on top of all the other forms of your application (including those with TopMost set) you can use the Show() method overload that takes a parameter of type MessageBoxOptions and pass MessageBoxOptions.ServiceNotification as that parameter.

DialogResult result = MessageBox.Show("Configuration file was corrupted.\n\nDo you want to reset it to default and lose all configurations?", "Config File Corrupted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);

Solution 4

A simple approach for a top most MessageBox would be something like this:

using (var dummy = new Form() { TopMost = true })
{
    MessageBox.Show(dummy, text, title);
}

You don't have to actually display the dummy form.

Solution 5

I think there is no built-in feature to do that in .Net, but I suggest you to keep a reference of your TopMost form, and change it before showing each message, something like following :

    public static void ShowMessage(string message)
    {
        Component.InstanceOfTopMost.TopMost = false;
        MessageBox.Show(message);
        Component.InstanceOfTopMost.TopMost = true;
    }

Component is a static class which is holds a reference of your form which should be TopMost. The reason of this static class is you may want to use that form in several places, this way you can easily access to your Form. This is a simple method, you can change it based on your requirements.

Update :

        public class Component
        {
            public static Form2 InstanceOfTopMost { get; set; }
        }

Component is just a name give another name to that, because there is another .Net class named Component.

        var frm = new Form2();
        Component.InstanceOfTopMost = frm;
        frm.Show();

Hope this help.

Share:
34,618
modest and cute girl
Author by

modest and cute girl

Updated on July 15, 2022

Comments

  • modest and cute girl
    modest and cute girl almost 2 years

    I have two forms and I set one of the forms' TopMost property to true. Somewhere, while the program runs, I show a MessageBox, but since TopMost is set to true, when the MessageBox pops up it shows under the topmost form so I cannot see it.

    1. Is there any way that I make one of my forms always be on top, but when a MessageBox pops up, make the message box show on top of that specific form?

    2. Is it possible to give a location to the MessageBox so that it shows not in the middle but for example low down on the screen?

    3. Let's say that I have an orange colored form can I have a pink colored message box only for that specific application. I mean I am NOT referring to playing the windows color properties. (I don't want all message boxes to be pink.)