MessageBox.Show方法小结

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 中)

 

public static DialogResult Show (

       string text,

       string caption,

       MessageBoxButtons buttons

)

参数

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

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

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

返回值

DialogResult 值之一。

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

private void validateUserEntry()
{
 // Checks the value of the text.
    if(serverName.Text.Length == 0)
{    
// Initializes the variables to pass to the MessageBox.Show method.
    string message = "You did not enter a server name. Cancel this operation?";
    string caption = "Error Detected in Input";
    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
    DialogResult result;
 // Displays the MessageBox.
 result = MessageBox.Show(message, caption, buttons);
        if(result == DialogResult.Yes)
        {
// Closes the parent form.
               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

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

 
private void button1_Click(object sender, System.EventArgs e) {
   if(textBox1.Text == "") {
      MessageBox.Show("You must enter a name.", "Name Entry Error",
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
   }
   else {
      // Code to act on the data entered would go here.
   }
}

 

posted @ 2012-08-31 15:06  bevin-H  阅读(560)  评论(0编辑  收藏  举报