MessageBox 类,不一样的感觉
我们在使用messagebox时,通常采用下面的最简化的方式:
MessageBox.Show("Hello World!");
当我们为messagebox添加标题时,可以这样做:
MessageBox.Show("Hello World!", "A Message");
也可以使用System.Windows.Forms.MessageBoxButtons enumeration为messagebox添加按钮。
MessageBox.Show("Hello World!", "A Message", MessageBoxButtons.OKCancel);
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
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);
The table below shows the different icons that you can use for your message box.
Icon | Member | Usage |
---|---|---|
Asterisk Information |
Used when showing information to the user. | |
Error Hand Stop |
Used when showing error messages. | |
Exclamation Warning |
Used when showing warning messages. | |
Question | Used when asking a question to the user. |
You can use the MessageBoxIcon.None to indicate that the message box will have no icon.
The MessageBoxDefaultButton enumeration tells which of the button is the default, that is, the one that is pressed when the enter key in the keyboard is pushed. It has only 4 member which are Button1, Button2, Button3, Button4. For example, in a message box that has an OK and a Cancel buttons, using MessageBoxDefaultButton.Button1 will make the OK button as the default. When the message box is shown and you pressed Enter in the keyboard, the OK button is pressed.
MessageBox.Show("Hello World!", "A Message",
MessageBoxButtons.OKCancel, MessageBoxDefaultButton.Button1);