Question

I've some MessageBox that I code like this:

MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Message","Title", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

For a better example, I do this for the FormClosing Event:

private void Example_FormClosing(object sender, FormClosingEventArgs e){
MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}

But, almost every time I've to change of Window on my computer (like return on Visual Studio) before seeing my messagebox and it's not user-friendly and really annoying.

I verified that my principal form was not in TopMost=true, I tried just the TopMost or just the TopLevel,the StartPosition=FormStartPosition.CenterScreen but nothing worked.

[Update]

I tried:

 private void Example_FormClosing(object sender, FormClosingEventArgs e){
    MessageBox.Show(this.Owner, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
    }

I'd like to have my messageBox on the front of my window and not have to change of window to see it because it's like behind the current window.

Have you an idea to resolve this problem?

Was it helpful?

Solution 2

Given an instance of your Form, you can call a MessageBox like this:
MessageBox.show(form, "Message", "Title"); (Check the doc for other parameters.)

However if you want to call this from a background thread (e.g.: BackgroundWorker) you have to use Form.Invoke() like this:

form.Invoke((MethodInvoker)delegate
{
   MessageBox.show(form, "Message", "Title");
});

OTHER TIPS

Do it like this:

MessageBox.Show(
    "Message", 
    "Title", 
    MessageBoxButtons.YesNo, 
    MessageBoxIcon.Warning, 
    MessageBoxDefaultButton.Button1, 
    MessageBoxOptions.DefaultDesktopOnly);

It will put it in front of all other windows, including those from other processes (which is what I think you're asking for).

The critical parameter is MessageBoxOptions.DefaultDesktopOnly. Note that this will parent the message box to the default desktop, causing the application calling MessageBox.Show() to lose focus.

(You should really reserve this behaviour for critical messages.)

Alternatively, if your application has a window, call this.BringToFront() before showing the message box by calling MessageBox.Show() with the first parameter set to this. (You'd call this from the window form class).

I've answered this here (but since it's a fairly small answer, I'll replicate it):

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

You're setting the MessageBox owner to a new form that hasn't been shown. Instead of new Form(){TopMost=true, TopLevel=True}, refer to an instance of an existing form that you want the MessageBox on top of.

Further to Lars' answer, I had the same problem and Lars' method worked, but if I then popped up a message from elsewhere that wasn't on top, switched to it, and then the message was called using Lars' method again, it would no longer be on top.

This is the variation that I came up with that works well for me, hope you find it helpful:

This is run from a separate thread, that has a reference to the main application form instance

//Show message on top of all other forms
MainFormInstance.Invoke((MethodInvoker)delegate
{
    Form popup = new Form() { TopMost = true, TopLevel = true };
    MessageBox.Show(popup, "Message", "Title");
});

Try to write generalize logic as:-

public static DialogResult ShowMessage(Form Parent, string Text, string Caption, MessageBoxButtons Buttons, MessageBoxIcon Icon, MessageBoxDefaultButton DefaultButton)
{
    if (Parent != null && Parent.InvokeRequired)
        return (DialogResult) Parent.Invoke(((Func<DialogResult>))(() => MessageBox.Show(Text, Caption, Buttons, Icon, DefaultButton)));
    else
        return (MessageBox.Show(Text, Caption, Buttons, Icon, DefaultButton));
}

I have been playing around with this and running multiple tests on Windows 7/8.1/10 then finally come to a working method to display the message box on top in all the systems mentioned above.

//Create an Empty Form with TopMost & TopLevel attributes.
Form popup = new Form() { TopMost = true, TopLevel = true };

//Running MessageBox on a different Thread

              Invoke((MethodInvoker)delegate {

     DialogResult dialogResult = MessageBox.Show(popup, "Custom Message Here", MessageBoxButtons.YesNo);
              if (dialogResult == DialogResult.Yes)
              {

                  //do something
              }
              else if (dialogResult == DialogResult.No)
              {
                  //do something else
              }

//Or a casual message box

//MessageBox.Show(popup, "Custom Message Here", "Alert");

 });

just do that as normal (MessageBox.Show(Message);) it's already topmost.

see here and here for information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top