Message Box C#-Trickcode

Message Box C#,System.Windows.Forms.MessageBox,MessageBox class
Share it:
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 it:

C#

Windows Forms

Post A Comment:

0 comments: