Form.CancelButton Property
Gets or sets the button control that is clicked when the user presses the ESC key.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Property Value
Type: System.Windows.Forms.IButtonControlAn IButtonControl that represents the cancel button for the form.
The cancel button for a form is the button control that is clicked whenever the user presses the ESC key. The button assigned to this property must be an IButtonControl that is on the current form or located within a container on the current form.
This property allows you to designate a default action to occur when the user presses the ESC key in your application. You can use this property to allow the user to quickly navigate a simple form by allowing them to simply press the ESC key to close a window without committing changes instead of manually clicking the cancel button with their mouse.
CancelButton may not work if another control on the form intercepts the ESC key. For example, if you have a ComboBox open on your form, ESC will close the ComboBox instead of closing the Form.
The IButtonControl object assigned to CancelButton must be visible on the form, or else pressing the ESC key will have no effect.
The following code example creates a new instance of a Form and calls the ShowDialog method to display the form as a dialog box. The example sets the FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBox, and StartPosition properties to change the appearance and functionality of the form to a dialog box. The example also uses the Add method of the form's Controls collection to add two Button controls. The example uses the HelpButton property to display a help button in the caption bar of the dialog box.
Available since 1.1