AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

Message Box C#-Trickcode

By pushpam abhishek
Listen to this article
The System.Windows.Forms.MessageBox is a static class and that is used to show message boxes for prompting confirmation, and warning users. To show a message box, simply call the Show method of the MessageBox class. The simplest version of the Show method is the one that accepts a string message as an argument.

MessageBox.Show("Hello World!");

 Message Box C#



You can also specify the title of the message box by using another overloaded version of the Show method.


MessageBox.Show("Hello World!", "A Message");

 Message Box C#

You can also change the buttons that will be shown in the message box if you don’t want to use the default OK button. You can be using the System.Windows.Forms.MessageBoxButtons enumeration.


MessageBox.Show("Hello World!", "A Message", MessageBoxButtons.OKCancel);
System.Windows.Forms.MessageBoxButtons

The table below shows the members of the MessageBoxButtons enumeration.

Member            Buttons Shown
AbortRetryIgnore                   Abort, Retry, Ignore
OK                  OK
OKCancel                  OK, Cancel
RetryCancel                  Retry, Cancel
YesNo                 Yes, No
YesNoCancel                 Yes, No, Cancel

The Show() method returns a value from the System.Windows.Forms.DialogResult enumeration. This is useful to determine what button you pressed in the message box. For example, if you click the “Yes” button in the message box, then the Show() method will return the value DialogResult.Yes.

DialogResult result;
result = MessageBox.Show("What is your choice?");

if (result == DialogResult.Yes)
{
   //You pressed the Yes button
}
if (result == DialogResult.No)
{
   //You pressed the No button
}

Please note that the Form class also has a DialogResult property. This is not the System.Windows.Forms.DialogResult.

You can also add an icon for your message box to further imply the purpose of the message. You do this by using the members of the MessageBoxIcon enumeration.

MessageBox.Show("Hello World!", "A Message", 
   MessageBoxButtons.OK, MessageBoxIcon.Information);

                             Share this article with your friends

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments