Each form has an AcceptButton property and a CancelButton property. Whichever button you assign to the AcceptButton property will be "pressed" when the user hits the ENTER key and whichever you assign to the CancelButton property will be "pressed" when the user hits the ESC key.
Note that when you assign a button to the CancelButton property, its DialogResult property is set to Cancel by default. This means that if the ESC key is pressed or the Cancel button is clicked, the form's DialogResult property will be set to Cancel, it will be closed automatically and, if it was called using ShowDialog, that function will return Cancel.
When you assign a button to the AcceptButton property, its DialogResult property is unchanged. If you want the form to close automatically when it is clicked, you should set it's DialogResult property to an appropriate value, which would usually be OK. If you want to do some processing before closing the form, you can still do this in the button's Click event handler. If you may or may not want to close the form automatically, you should leave the button's DialogResult property set to None and only set the form's DialogResult property to something like OK if you want to close the form. Note that whenever a form's DialogResult property is set, like when a button that has its own DialogResult property set is clicked, the current method is completed and the form is closed. If it was called using ShowDialog, that function will return the value of the form's DialogResult property.
Enter your message below
Sign in or Join us (it's free).