Freigeben über


MessageBox-Klasse

Zeigt ein Meldungsfeld an, das Text, Schaltflächen und Symbole mit Informationen und Anweisungen für den Benutzer enthalten kann.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Class MessageBox
'Usage
Dim instance As MessageBox
public class MessageBox
public ref class MessageBox
public class MessageBox
public class MessageBox

Hinweise

Es kann keine neue Instanz der MessageBox-Klasse erstellt werden. Sie können ein Meldungsfeld anzeigen, indem Sie die static-Methode MessageBox.Show aufrufen. Titel, Meldung, Schaltflächen und Symbole, die in dem Meldungsfeld angezeigt werden, werden durch Parameter bestimmt, die Sie an diese Methode übergeben.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie Benutzer mithilfe einer MessageBox über einen fehlenden Eintrag in einer TextBox informiert werden. Für dieses Beispiel muss die Methode aus einem vorhandenen Formular mit einem Button und einer TextBox aufgerufen werden.

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

Im folgenden Codebeispiel wird veranschaulicht, wie dem Benutzer eine mit Ja oder Nein zu beantwortende Frage gestellt werden kann und wie anschließend je nach der Antwort eine Entscheidung getroffen wird.

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

Vererbungshierarchie

System.Object
  System.Windows.Forms.MessageBox

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

MessageBox-Member
System.Windows.Forms-Namespace
Show