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.
You can also specify the title of the message box by using another overloaded version of the Show method.
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.
The table below shows the members of the MessageBoxButtons enumeration.
MessageBox.Show("Hello World!");
|
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");
|
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);
|
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 } |
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
Post A Comment:
0 comments: