MessageBox (Clase)
Actualización: noviembre 2007
Muestra un cuadro de mensaje que puede contener texto, botones y símbolos que informan e instruyen al usuario.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
No se puede crear una nueva instancia de la clase MessageBox. Para mostrar un cuadro de mensaje, llame al método staticMessageBox.Show. El título, el mensaje, los botones y los iconos que aparecen en el cuadro de mensaje están determinados por los parámetros que se pasan a este método.
En el siguiente ejemplo de código se muestra cómo se utiliza un objeto MessageBox para informar al usuario sobre la ausencia de una entrada en un objeto TextBox. Este ejemplo requiere que se llame al método desde un formulario existente que incluya un objeto Button y un objeto TextBox.
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
En el ejemplo de código siguiente se muestra cómo se le formula al usuario una pregunta cuya respuesta es sí o no y cómo se toma una decisión en función de la respuesta.
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 == System.Windows.Forms.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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile para Smartphone, Windows Mobile para Pocket PC
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.