-1

I am working on c# winforms application. I am actively working with 2 monitors namely primary and secondary. When I run the application, Message Box always pops up on the primary monitor irrespective on which monitor I run the application.

Here below shown are the 2 ways I tried but Message Box pops up on primary monitor:

1.

MessageBox.Show("Test Success", "Success", MessageBoxButtons.OK, 
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, 
MessageBoxOptions.ServiceNotification);

2.

MessageBox.Show("Test Success", "Success", MessageBoxButtons.OK, 
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, 
MessageBoxOptions.DefaultDesktopOnly);

Is there any way I can display Message Box on the monitor I run the application dynamically?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Varma
  • 13
  • 1
  • 4
  • 2
    By _not_ providing the MessageBoxOptions? – CodeCaster Mar 28 '19 at 14:34
  • similar: https://stackoverflow.com/questions/29700586/how-do-i-do-messagebox-show-on-primary-screen – Falco Alexander Mar 28 '19 at 14:34
  • If you have multiple forms and they are displayed on different monitors, then to display messagebox on corresponding monitor you have to use [overload](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox.show) which allow specify owner (first parameter where you simply have to supply current form `this`). – Sinatr Mar 28 '19 at 15:04

1 Answers1

2

as already stated in the comment: don't specify the MessageBoxOptions. Simply call it like this:

MessageBox.Show("Test Success", "Success", MessageBoxButtons.OK, 
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

and it will appear on the same monitor as your application, right in front of it.

I want to display the Message Box on top of any other apps I have opened.

Then you should force the Form that is calling the MessageBox to the surface. Call this before showing the message box:

this.TopMost = true;
MessageBox.Show(...
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • The above solution works! But the concern here is, while the application is doing some background process before displaying the Message Box, and during this period if I open any other applications for eg. chrome, visual studio etc.., after finishing the process, the Message Box is hidden under the applications I opened(in this case chrome, visual studio). Here I want to implement in a way that I want to display the Message Box on top of any other apps I have opened. – Varma Mar 28 '19 at 15:08
  • @Varma OK, I understand. But actuall this would be a second question ;) – Mong Zhu Mar 28 '19 at 15:47
  • Applying "this.TopMost = true;" resolved my issue partially because I am not able to bring up any app(chrome, VS etc..) on top of this form. So until the process is finished we stay on the same screen. In order to resolve this issue I have implemented below code: MessageBox.Show("Test Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, **(MessageBoxOptions)0x40000**); Thanks much for your help :) – Varma Mar 28 '19 at 18:28