MessageBox-Klasse
Assembly: System.Windows.Forms (in system.windows.forms.dll)
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.
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 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. } }
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 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.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
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.