Leer en inglés

Compartir a través de


Form.CancelButton Propiedad

Definición

Obtiene o establece el control de botón que se activará cuando el usuario presione la tecla ESC.

public System.Windows.Forms.IButtonControl CancelButton { get; set; }
public System.Windows.Forms.IButtonControl? CancelButton { get; set; }

Valor de propiedad

IButtonControl que representa el botón Cancelar del formulario.

Ejemplos

En el ejemplo siguiente se crea una nueva instancia de y Form se llama al ShowDialog método para mostrar el formulario como un cuadro de diálogo. En el ejemplo se establecen las FormBorderStylepropiedades , , AcceptButtonMinimizeBoxCancelButton, , MaximizeBoxy StartPosition para cambiar la apariencia y la funcionalidad del formulario a un cuadro de diálogo. En el ejemplo también se usa el Add método de la colección del Controls formulario para agregar dos Button controles. En el ejemplo se usa la HelpButton propiedad para mostrar un botón de ayuda en la barra de títulos del cuadro de diálogo.

public void CreateMyForm()
{
   // Create a new instance of the form.
   Form form1 = new Form();
   // Create two buttons to use as the accept and cancel buttons.
   Button button1 = new Button ();
   Button button2 = new Button ();
  
   // Set the text of button1 to "OK".
   button1.Text = "OK";
   // Set the position of the button on the form.
   button1.Location = new Point (10, 10);
   // Set the text of button2 to "Cancel".
   button2.Text = "Cancel";
   // Set the position of the button based on the location of button1.
   button2.Location
      = new Point (button1.Left, button1.Height + button1.Top + 10);
   // Set the caption bar text of the form.   
   form1.Text = "My Dialog Box";
   // Display a help button on the form.
   form1.HelpButton = true;

   // Define the border style of the form to a dialog box.
   form1.FormBorderStyle = FormBorderStyle.FixedDialog;
   // Set the MaximizeBox to false to remove the maximize box.
   form1.MaximizeBox = false;
   // Set the MinimizeBox to false to remove the minimize box.
   form1.MinimizeBox = false;
   // Set the accept button of the form to button1.
   form1.AcceptButton = button1;
   // Set the cancel button of the form to button2.
   form1.CancelButton = button2;
   // Set the start position of the form to the center of the screen.
   form1.StartPosition = FormStartPosition.CenterScreen;
   
   // Add button1 to the form.
   form1.Controls.Add(button1);
   // Add button2 to the form.
   form1.Controls.Add(button2);
   
   // Display the form as a modal dialog box.
   form1.ShowDialog();
}

Comentarios

El botón Cancelar de un formulario es el control de botón en el que se hace clic cada vez que el usuario presiona la tecla ESC. El botón asignado a esta propiedad debe ser un IButtonControl que se encuentra en el formulario actual o que se encuentra dentro de un contenedor del formulario actual.

Esta propiedad permite designar una acción predeterminada para que se produzca cuando el usuario presiona la tecla ESC en la aplicación. Puede usar esta propiedad para permitir al usuario navegar rápidamente por un formulario sencillo, ya que permite simplemente presionar la tecla ESC para cerrar una ventana sin confirmar cambios en lugar de hacer clic manualmente en el botón cancelar con el mouse.

CancelButton es posible que no funcione si otro control del formulario intercepta la clave ESC. Por ejemplo, si tiene abierto ComboBox en el formulario, ESC cerrará en ComboBox lugar de cerrar el formulario.

El IButtonControl objeto asignado a CancelButton debe estar visible en el formulario o, de lo contrario, presionar la tecla ESC no tendrá ningún efecto.

Se aplica a

Producto Versiones
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Consulte también