Message Box in C#

In this article, we will learn how to use message box in windows form in C#.

Let's create windows form.

In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

First of all, we'll know about the "message box".

MessageBox displays a message box that can contain text, buttons, and symbols that inform and instruct the user. We'll discuss properties and function and events of the message write down.

Simple MessageBox

The simplest form of a MessageBox is a dialog with a text and OK button. When you click OK button, the box disappears.

The following code snippet creates a simple Message Box.

string msg = "Hello World";
MessageBox.Show(msg);
simple message box

MessageBox with title properties

The following code snippet creates a Message Box with title

string msg = "Hello World";
string title = "Message";
MessageBox.Show(msg,title);
message box with title

MessageBox with buttons

Specifies a "MessageBoxButton" enumeration value that determines a corresponding button to be displayed the message box.

The following code snippet creates a Message Box with buttons.

  1. OK - Shows the OK button.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.OK;
    MessageBox.Show(msg,title,mbb);
    
    message box with Ok button
  2. OKCancel - Shows the OK and Cancel buttons.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.OKCancel;
    if (DialogResult.OK == MessageBox.Show(msg, title, mbb))
    {
      //code here
    }
    else
    {
      //code here
    }
    
    message box with Ok and Cancel buttons
  3. RetryCancel - Shows the Retry and Cancel buttons.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.RetryCancel;
    if (DialogResult.Retry == MessageBox.Show(msg, title, mbb))
    {
      //code here
    }
    else
    {
      //code here
    }
    
    message box with retry and cancel buttons
  4. AbortRetryIgnore - Shows the Abort,Retry and Ignore buttons.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.AbortRetryIgnore;
    DialogResult dr = MessageBox.Show(msg, title, mbb);
    if (DialogResult.Abort == dr)
    {
      //code here
    }
    else if (DialogResult.Retry == dr)
    {
      //code here
    }
    else
    {
      //code here
    }
    
    message box with abort,retry and ignore buttons
  5. YesNo - Shows the Yes and No buttons.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.YesNo;
    DialogResult dr = MessageBox.Show(msg, title, mbb);
    if (DialogResult.Yes == dr)
    {
      //code here
    }
    else
    {
      //code here
    }
    
    message box with yes and no buttons
  6. YesNo - Shows the Yes and No buttons.

    string msg = "Hello World";
    string title = "Message";
    MessageBoxButtons mbb = MessageBoxButtons.YesNoCancel;
    DialogResult dr = MessageBox.Show(msg, title, mbb);
    if (DialogResult.Yes == dr)
    {
      //code here
    }
    else if (DialogResult.No == dr)
    {
      //code here
    }
    else
    {
      //code here
    }
    
    message box with yes,no and cancel buttons

MessageBox with Icons

Specifies a "MessageBoxIcon" enumeration value that determines a corresponding icon to be displayed the message box.

The following code snippet creates a Message Box with icons.

  1. Asterisk - Shows an icon displaying a lowercase letter i in a circle.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Asterisk;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with asterisk icon
  2. Error - Shows an icon displaying a white X in a circle with a red background.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Error;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with error icon
  3. Exclamation - Shows an icon displaying an exclamation mark in a triangle with a yellow background.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Exclamation;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with exclamation icon
  4. Hand - Shows an icon displaying a white X in a circle with a red background.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Hand;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with hand icon
  5. Information - Shows an icon displaying a lowercase letter i in a circle.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with information icon
  6. None - Does not show any icon.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.None;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with None icon
  7. Question - Shows an icon displaying a question mark in a circle.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Question;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with question icon
  8. Stop - Shows an icon displaying a white X in a circle with a red background.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Stop;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with stop icon
  9. Warning - Shows an icon displaying an exclamation mark in a triangle with a yellow background.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Warning;
    MessageBox.Show(msg, caption,btn,icon);
    
    message box with warning icon

MessageBox with default Button

Specifies a "MessageBoxDefaultButton" enumeration value that determines a corresponding value that determines a default button to be displayed in the message box.

The following code snippet creates a Message Box with defaultbutton.

  1. Button1 - Makes the first button in the message box as the default button(focusable).

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button1;
    MessageBox.Show(msg, caption,btn,icon,button1);
    
    message box with defaultbutton1
  2. Button2 - Makes the second button in the message box as the default button(focusable).

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button2 = MessageBoxDefaultButton.Button2;
    MessageBox.Show(msg, caption,btn,icon,button2);
    
    message box with defaultbutton2
  3. Button3 - Makes the third button in the message box as the default button(focusable).

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button3 = MessageBoxDefaultButton.Button3;
    MessageBox.Show(msg, caption,btn,icon,button3);
    
    message box with defaultbutton3

MessageBox with Options

Specifies a "MessageBoxOptions" enumeration value that determines display and association options for the messages box.

The following code snippet creates a Message Box with Options.

  1. DefaultDesktopOnly - Displays the message box on the default desktop of Windows.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button3;
    MessageBoxOptions option=MessageBoxOptions.DefaultDesktopOnly;
    MessageBox.Show(msg, caption,btn,icon,button1,option);
    
    message box with options defaultdesktoponly
  2. RightAlign - Displays the message box text as right-aligned.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button3;
    MessageBoxOptions option=MessageBoxOptions.RightAlign;
    MessageBox.Show(msg, caption,btn,icon,button1,option);
    
    message box with options rightalign
  3. RtlReading - Specifies that the message box text is displayed in the right to left reading order.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button3;
    MessageBoxOptions option=MessageBoxOptions.RtlReading;
    MessageBox.Show(msg, caption,btn,icon,button1,option);
    
    message box with options RtlReading
  4. ServiceNotification - Displays the message box on the active desktop when a service makes a call, notifying a user of an event.

    string msg = "Hello World";
    string caption = "Message";
    MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button3;
    MessageBoxOptions option=MessageBoxOptions.ServiceNotification;
    MessageBox.Show(msg, caption,btn,icon,button1,option);
    
    message box with options ServiceNotification

MessageBox with helpfilepath

Specifies the path and name of the help file to be displayed when a user clicks the help button.

The following code snippet creates a Message Box with helpfilepath.

string msg = "Hello World";
string caption = "Message";
MessageBoxButtons btn = MessageBoxButtons.AbortRetryIgnore;
MessageBoxIcon icon = MessageBoxIcon.Information;
MessageBoxDefaultButton button1 = MessageBoxDefaultButton.Button3;
MessageBox.Show(msg, caption,btn,icon,button1,0,"https://guriasoft.com/");


Write a comment


Name Email Message