Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key Press Event

Status
Not open for further replies.

agooddatamgr

Technical User
Aug 4, 2009
4
US
Hi everyone,

I have a Login form with LOGIN button. I want to set the Key Press event in the properties of the form when I click the enter button, focus is on the login button.

How is the Key Press event coded in Vb to do this?

When I press the enter button, I want to be pressing the Login button.

Any help is appreciated.

agooddatamgr
 
An easy solution is to set the form's AcceptButton property to the login button, this way when the user presses enter the login button event will fire.

Another option is to set the form's KeyPreview property to true and then I'd suggest using the form's KeyDown event instead of the KeyPress event, then trap for the enter key like:

Code:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Enter Then
            Button1.PerformClick()
        End If

    End Sub
 
When I press the enter button, I want to be pressing the Login button.
Do you mean
When I press the enter key, I want to be clicking the Login button.

If so, simply set the form's AcceptButton property to the LOGIN button.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top