tags:

views:

3569

answers:

5

How can I capture enter keypress anywhere on my form and force it to fire the submit button event?

+2  A: 

If I remember correctly, the Form has a KeyPreview property that you can use to intercept the keypress.


Just looked at this page to verify it.

itsmatt
+14  A: 

If you set your Form's AcceptButton property to one of the Buttons on the form, you'll get that behaviour by default.

Otherwise set the KeyPreview property to True on the form and handle its KeyDown event. You can check for the Enter key and take the necessary action.

Matt Hamilton
A: 

Set the KeyPreview attribute on your form to True, then use the KeyPress event at your form level to detect the Enter key. On detection call whatever code you would have for the "submit" button.

Bill
+5  A: 

You can designate a button as the "AcceptButton" in the Form's properties and that will catch any "Enter" keypresses on the form and route them to that control.

See this MSDN article and note the few exceptions it outlines (multi-line text-boxes, etc.)

bouvard
Thanks for the link, those exceptions where holding me up.
typoknig
A: 

As previously stated, set your form's AcceptButton property to one of it's buttons AND set the DialogResult property for that button to DialogResult.OK, in order for the caller to know if the dialog was accepted or dismissed.

Sorin Comanescu