Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Messagebox.show() on top of other application using c#

Tags:

c#

People also ask

What is MessageBox class explain MessageBox () in detail?

A message box is a prefabricated modal dialog box that displays a text message to a user. You show a message box by calling the static Show method of the MessageBox class. The text message that is displayed is the string argument that you pass to Show.

What is the 2nd parameter in MessageBox show ()?

The first parameter msg is the string displayed in the dialog box as the message. The second and third parameters are optional and respectively designate the type of buttons and the title displayed in the dialog box. MsgBox Function returns a value indicating which button the user has chosen.

Is MessageBox show a modal dialog window?

Definition. Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user.

What is MessageBox show in C#?

MessageBox is a class in C# and Show is a method that displays a message in a small window in the center of the Form. MessageBox is used to provide confirmations of a task being done or to provide warnings before a task is done. Create a Windows Forms app in Visual Studio and add a button on it.


I tried the solution provided by donutboy and it doesn't seem to accept 0x40000 (or 40000) as a valid option as a MessageBoxOptions Enum value.

However I have found that using MessageBoxOptions.DefaultDesktopOnly has the same effect and keeps the MessageBox on top until it is confirmed by the user. ie.

MessageBox.Show("Hello there", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);

This is likely the simplest native solution on offer.


There's a better solution, without creating a new form.

MessageBox.Show("Message Text", "Header", MessageBoxButtons.OK, MessageBoxIcon.None, 
     MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);  // MB_TOPMOST

The 0x40000 is the "MB_TOPMOST"-Flag.


Another easy way to handle this:

MessageBox.Show(new Form { TopMost = true }, "This is TopMost", "TopMost", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);