MessageBox+类

来源:互联网 发布:linux虚拟机插优盘 编辑:程序博客网 时间:2024/06/05 16:54

我们在使用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);

Message Box

The table below shows the members of the MessageBoxButtons enumeration.

MemberButtons ShownAbortRetryIgnoreAbort, Retry, IgnoreOKOKOKCancelOK, CancelRetryCancelRetry, CancelYesNoYes, NoYesNoCancelYes, 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);

Message Box

The table below shows the different icons that you can use for your message box.

IconMemberUsageinformationAsterisk
InformationUsed when showing information to the user.errorError
Hand
StopUsed when showing error messages.warningExclamation
WarningUsed when showing warning messages.questionQuestionUsed 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 Button1Button2Button3Button4. 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);
原创粉丝点击