Dan Bright's .NET Weblog

Default Button for WinForm Apps

I am stumped, and I was hoping the community here could help me with (what should be) a very simple question.

I am trying to catch the Enter key when the cursor is in a particular TextBox, much like the DefaultButton server control for ASP.NET, but I am working with a WinForm App.

Any ideas?

Comments

Greg Robinson said:

You can fake an Escape and Enter in Winforms by telling the Form which button is the Cancel button and which is the Enter button.

Me.CancelButton =
Me.AcceptButton =
# April 14, 2003 10:53 AM

Phil Weber said:

Dan: Put a button on the form, and set the form's AcceptButton property to the name of that button. The Enter key will then fire the button's Click event.
# April 14, 2003 10:55 AM

Royo said:

Another tip: you can set a button's DialogResult property to OK or cancel. in conjunction with the Form.AcceptButton and Form.CancelButton properties set - you save quite a bit of plumbing code!
# April 14, 2003 4:07 PM

andrew said:

1. redirect your key process to form:

Private Sub frmConnect_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub

2. do your key actions

' key form actions
Private Sub frmConnect_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case e.KeyCode.Enter
Me.DoBtnOKClick()
Case e.KeyCode.Escape
Me.DoBtnCancel()
End Select
End Sub
# April 14, 2003 9:06 PM

Pooran Prasad said:

Hi Dan,
Hope this link will help.. [ http://www.allasp.net/enterkey.aspx ]
Have a great day :)
Pooran
# December 2, 2003 5:55 AM

kkkk said:


lll
# February 12, 2004 9:17 AM

Darek Wax said:

Rather nice blog you've got here. Thanks for it. I like such themes and everything connected to them. I would like to read more soon.

# January 19, 2010 7:39 PM

Bella Hakkinen said:

Rather nice blog you've got here. Thanks for it. I like such topics and everything connected to this matter. I would like to read a bit more soon.

Bella Hakkinen    

<a href="milanescorts.com/">ambra escort accompagnatrice milano</a>

# March 18, 2011 1:41 AM

Bella Benedict said:

It is rather interesting for me to read this article. Thanks the author for it. I like such themes and everything connected to this matter. I would like to read a bit more on that blog soon.  

Bella Benedict    

<a href="www.pickescort.com/">escort service uk</a>

# April 9, 2011 2:31 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)