MessageBox (Clase)
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 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter
Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.