How to Use MessageBox.Show Method in C#

MessageBox.Show():
 
MessageBox is a class and Show is a function 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.
 
Example
 
 
Figure 1 Windows Form
 

Figure 2 Code Snippet
 
OUTPUT

 
Figure 3 Showing the output
 
Note

By default the OK Button will be shown.
 

Figure 4 Second and third argument MessageBoxIcon  
 
Figure 5 Fourth argument specifies MessageBoxIcon
 
Dialog Result
 
DialogResult is an enumeration of the possible return values of a dialog box including a MessageBox.
 
 
Figure 6
Design phase
 
Form1.cs
  1. private void button1_Click(object sender, EventArgs e)      
  2.        {      
  3.            DialogResult d;      
  4.           d=MessageBox.Show("Welcome to C# Corner","Learn C#",MessageBoxButtons.YesNo,MessageBoxIcon.Information);      
  5.            if(d==DialogResult.Yes)      
  6.            {      
  7.                Close();      
  8.            }      
  9.        }      
 OUTPUT
 
 
Figure 7 Final output
 
Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all