Tip for Windows Forms developers - use Form.CancelButton on your modal dialogs

There's a problem that I encounter with unpleasant frequency using a variety of Windows Forms applications. The problem is that dialog boxes don't dismiss when the escape key is pressed. It's a small thing, but for a keyboard jockey like me, it's very annoying.

The problem occurs when the CancelButton property on the Form isn't set. In a properly coded modal dialog, you set this property to be your cancel button, and the framework handles dismissing the dialog when the escape key is pressed. However, if you directly set the DialogResult of the cancel button to Cancel but don't set the CancelButton property on the form, clicking on the cancel button will dismiss the dialog, but hitting escape will not. If you're only testing with the mouse, everything appears to work as expected.

I reported this as a bug against Whidbey, but MS doesn't seem inclined to change it. So please, people, set that CancelButton property. Take pity on a poor keyboard addicted soul.

1 Comment

  • Obviously you're working with the same set of developers I am: weaned during the 1997- internet boom, so all they know is HTML GUI conventions (such as they are).



    I actually had one guy say that checkboxes should *only* check if you actually click the 16x16 box itself. Exactly like they do in default HTML.



    Lunacy.

Comments have been disabled for this content.