您的位置:首页 > 其它

MessageBox.Show方法小结

2012-08-31 15:06 183 查看
MessageBox.Show方法(String,String,MessageBoxButtons)
http://msdn.microsoft.com/zh-cn/library/0x49kd7z(v=vs.80).aspx
显示具有指定文本、标题和按钮的消息框。

命名空间:System.Windows.Forms

程序集:System.Windows.Forms(在system.windows.forms.dll中)

publicstaticDialogResultShow(

stringtext,

stringcaption,

MessageBoxButtonsbuttons

)

参数

Text要在消息框中显示的文本。

Caption要在消息框的标题栏中显示的文本。

ButtonsMessageBoxButtons值之一,可指定在消息框中显示哪些按钮。

返回值

DialogResult值之一。

下面的代码示例演示如何使用受此Show重载支持的选项显示一个MessageBox。验证字符串变量ServerName为空后,此示例显示一个MessageBox,为用户提供取消该操作的选项。如果Show方法的返回值计算为Yes,则将关闭显示MessageBox的窗体。

privatevoidvalidateUserEntry()

{

//Checksthevalueofthetext.

if(serverName.Text.Length==0)

{

//InitializesthevariablestopasstotheMessageBox.Showmethod.

stringmessage="Youdidnotenteraservername.Cancelthisoperation?";

stringcaption="ErrorDetectedinInput";

MessageBoxButtonsbuttons=MessageBoxButtons.YesNo;

DialogResultresult;

//DisplaystheMessageBox.

result=MessageBox.Show(message,caption,buttons);

if(result==DialogResult.Yes)

{

//Closestheparentform.

this.Close();

}

}


}

MessageBoxButtons枚举

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.messageboxbuttons(v=vs.80).aspx

指定若干常数,用以定义MessageBox上将显示哪些按钮

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在system.windows.forms.dll中)

AbortRetryIgnore

消息框包含“中止”、“重试”和“忽略”按钮。

OK

消息框包含“确定”按钮。

OKCancel

消息框包含“确定”和“取消”按钮。

RetryCancel

消息框包含“重试”和“取消”按钮。

YesNo

消息框包含“是”和“否”按钮。

YesNoCancel

消息框包含“是”、“否”和“取消”按钮。


privatevoidbutton1_Click(objectsender,System.EventArgse){

if(textBox1.Text==""){

MessageBox.Show("Youmustenteraname.","NameEntryError",

MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

}

else{

//Codetoactonthedataenteredwouldgohere.

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: