bharathivkmani
bharathivkmani

Reputation: 59

vb.net enter key press on a button

I have a form with a button and when i click TAB key the button got selected but when I click ENTER key this code is not executed.My code is,

Private Sub Button1_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
If Asc(e.KeyChar) = 13 Then
 MsgBox("DO SOMETHING") 
End If 
End Sub 

Upvotes: 0

Views: 14372

Answers (2)

Lab Tipp
Lab Tipp

Reputation: 11

I found solution from this

Code :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SendKeys.Send("{ENTER}")
End Sub

it work for me.

Upvotes: 1

Nadeem_MK
Nadeem_MK

Reputation: 7689

I don't think you have anything to code.. If your focus is on a button and you press ENTER, the Button1_Click event will fire..

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox("DO SOMETHING")
End Sub

That's the case if your focus is on the button. And if ever you wish to fire your event wherever your focus is, the your code should be fitted in the Form1_KeyPress() event

Upvotes: 2

Related Questions