Enter Key pressed event

It depends on what control you want the user to press enter on.
If it's the form, add code in the Form's KeyDown or KeyPress
event handler. If it's for a textbox, then add the code for THOSE
event handlers. You can have multiple controls call the same
event handler if you want, too.

Another thing you can do is to set the form's AcceptButton
property to the button that you want to call when the user hits
enter. Then the user can click that button OR hit enter, and
whatever code is in that button's click event handler will be called.

[edit]
I forgot to mention... the KeyChar you're looking for in the
KeyPress event is the Carriage Return character, ControlChars.Cr
[/edit]
 
Hi

Two ways of doing this:

1) Use a event delegate to listen to all your form's control's keydown events, e.g.

Visual Basic:
Private Sub OnKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown

if e.KeyCode = Keys.Enter then
'your code here.....
end if

    End Sub


2) or you could use a accept button on the form.

Visual Basic:
'you can set this in the designer window
Me.AcceptButton = Button1

'this event will fire either when the user clicks on the button or hit the enter key.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'your code here...
    End Sub


Hope it helps

PS. Let me know if you want the C# code for the above

Cheers
 
Back
Top