views:

377

answers:

3

hi all

i just want to alert when user hit "enter" key.

i tried this one in keyup event,

If e.KeyCode = Keys.Enter Then
            MsgBox("msg")
        End If

it didnt work, is that wrong ?

thx

A: 

It really depends what context you are applying to. The KeyUp event will only fire on a particular control, and bubble up to it's parent controls. However, if focus is not set on the control you are handling the event on then the event will not fire.

Tim
actually there is just a button on my form, and i just want to handle enter hits to that button, it should be easiest :
Ahmet vardar
A: 

Just need to makes sure you put the code inside a sub that Handles Me.KeyDown

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            MsgBox("Enter Pressed")
        End If
    End Sub
Martin Beeby
yeah i did that, no luck, there must be sth missing
Ahmet vardar
I created a new project with the code above and it worked fine.Do you have any other code in the page that could be capturing the same event?
Martin Beeby
+3  A: 

The Enter key has strictly defined use in UI design, it executes the "accept" action of a dialog. In the designer, select the form and set the AcceptButton to your button. No code is required.

Note that the CancelButton has a similar usage, it is hard-wired to the Escape key.

Hans Passant