Monday, June 4, 2007

Submitting forms on the enter key

Quite a while ago I was working on a form with a few textboxes and a submit button. I was nearly complete with the application but it didn't do one thing that I wanted, I had to either tab to the submit button and hit enter or click on it with the mouse. I'm fairly lazy and I know most people are too, so this little problem would most likely annoy quite a few people.

Well, after searching on google for a few hours and going over my past code (turns out I had found the solution a long time back, then forgot) and came up with the solution.

Code:
this.AcceptButton = button_Submit;

Seems simple enough, the real trick is to change the AcceptButton whenever you have a different form you want to submit. Then all of your various forms will respond accordingly and you don't have to attempt to handle any key downs or other messages to mimic this behavior.

No comments: