Enter Key pressed event

It depends on what control you want the user to press enter on.
If its the form, add code in the Forms KeyDown or KeyPress
event handler. If its 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 forms 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 buttons click event handler will be called.

[edit]
I forgot to mention... the KeyChar youre 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 forms controls keydown events, e.g.

Code:
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.

Code:
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
 
Great minds think alike!

I cant believe we both reccomended the same thing... wow.
 
Last edited by a moderator:
Back
Top