Leer en inglés

Compartir a través de


Form.AcceptButton Propiedad

Definición

Obtiene o establece el botón del formulario que se activa cuando el usuario presiona la tecla ENTRAR.

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

Valor de propiedad

IButtonControl que representa el botón que se usa como el botón para aceptar el formulario.

Ejemplos

En el ejemplo siguiente se crea una nueva instancia de Form y se llama al ShowDialog método para mostrar el formulario como un cuadro de diálogo. En el ejemplo se establecen las FormBorderStylepropiedades , AcceptButton, CancelButton, MinimizeBox, 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

Esta propiedad permite designar una acción predeterminada para que se produzca cuando el usuario presiona la tecla ENTRAR en la aplicación. 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.

Puede usar esta propiedad para permitir que el usuario navegue rápidamente por un formulario simple permitiendo simplemente presionar la tecla ENTRAR cuando termine, en lugar de hacer clic manualmente en el botón Aceptar con el mouse.

Es posible que el botón Aceptar no se active si el control seleccionado actualmente en el formulario intercepta la tecla ENTRAR y la procesa. Por ejemplo, un control de cuadro de texto de varias líneas permite presionar la tecla ENTRAR cuando se selecciona para insertar un nuevo carácter de línea en el control.

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