Form.CancelButton Propriété

Définition

Obtient ou définit le contrôle bouton qui est activé lorsque l'utilisateur appuie sur la touche Échap.

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

Valeur de propriété

IButtonControl représentant le bouton d'annulation du formulaire.

Exemples

L’exemple suivant crée une instance d’un Form et appelle la ShowDialog méthode pour afficher le formulaire sous forme de boîte de dialogue. L’exemple définit les FormBorderStylepropriétés , AcceptButton, CancelButtonMinimizeBox, MaximizeBox, et StartPosition pour modifier l’apparence et les fonctionnalités du formulaire en une boîte de dialogue. L’exemple utilise également la Add méthode de la collection du Controls formulaire pour ajouter deux Button contrôles. L’exemple utilise la HelpButton propriété pour afficher un bouton d’aide dans la barre de sous-titres de la boîte de dialogue.

C#
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();
}

Remarques

Le bouton Annuler d’un formulaire est le contrôle de bouton sur lequel on clique chaque fois que l’utilisateur appuie sur la touche Échap. Le bouton affecté à cette propriété doit être un IButtonControl qui se trouve sur le formulaire actif ou se trouve dans un conteneur sur le formulaire actif.

Cette propriété vous permet de désigner une action par défaut qui se produira lorsque l’utilisateur appuie sur la touche ÉCHAP dans votre application. Vous pouvez utiliser cette propriété pour permettre à l’utilisateur de naviguer rapidement dans un formulaire simple en lui permettant d’appuyer simplement sur la touche Échap pour fermer une fenêtre sans valider les modifications au lieu de cliquer manuellement sur le bouton Annuler avec sa souris.

CancelButton peut ne pas fonctionner si un autre contrôle du formulaire intercepte la clé ÉCHAP. Par exemple, si vous avez un ComboBox ouvert sur votre formulaire, ÉCHAP ferme le ComboBox au lieu de fermer le formulaire.

L’objet IButtonControl affecté à CancelButton doit être visible sur le formulaire, sinon, appuyer sur la touche ÉCHAP n’aura aucun effet.

S’applique à

Produit Versions
.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

Voir aussi