MessageBox 类

显示可包含文本、按钮和符号(通知并指示用户)的消息框。

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

语法

声明
Public Class MessageBox
用法
Dim instance As MessageBox
public class MessageBox
public ref class MessageBox
public class MessageBox
public class MessageBox

备注

无法创建 MessageBox 类的新实例。若要显示消息框,请调用 static 方法 MessageBox.Show。显示在消息框中的标题、消息、按钮和图标由您传递给该方法的参数确定。

示例

下面的代码示例演示如何使用 MessageBoxTextBox 中缺少的项通知用户。该示例要求从具有 ButtonTextBox 的现有窗体调用该方法。

Private Sub button1_Click(sender As Object, e As System.EventArgs)
    If textBox1.Text = "" Then
        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.
    End If
End Sub
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.
   }
}
private:
   void button1_Click( Object^ sender, System::EventArgs^ e )
   {
      if ( textBox1->Text->Equals( "" ) )
      {
         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.
      }
   }
protected void button1_Click(Object sender, System.EventArgs e)
{
    if (textBox1.get_Text().Equals("")) {
        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.
    } 
} //button1_Click

下面的代码示例演示如何询问用户一个是与否的问题,并根据响应做出决定。

Private Sub ValidateUserEntry()

    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "Error Detected in Input"
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays the MessageBox

        Result = MessageBox.Show(Message, Caption, Buttons)

        ' Gets the result of the MessageBox display.

        If Result = DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub
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();

        }

    }

}
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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
private void ValidateUserEntry()
{
    // Checks the value of the text.
    if (serverName.get_Text().get_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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.
        result = MessageBox.Show(this, message, caption, buttons);

        if (result.Equals(DialogResult.Yes)) {
            // Closes the parent form.
            this.Close();
        }
    }
} //ValidateUserEntry

继承层次结构

System.Object
  System.Windows.Forms.MessageBox

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

MessageBox 成员
System.Windows.Forms 命名空间
Show