Password Enter

Hi all

I have made a form to log users in that it has 2x labels 2x boxes (user name and password) 2x button (logon and cancel).

How to have a commend come of the the enter button when press in the password boxes.

Eg. you type in your user name and then password and press enter... so you login.

At the moment the user has to click logon.

Who is Participating?
You need a cathc KeyPress event on Password TextBox.
when e.KeyChar = ControlChars.Cr then do the same action as Logon Button.

    Private Sub TxtPwd_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPwd.KeyPress
       If e.KeyChar = ControlChars.Cr Then
          e.Handled = True
Avinash DesaiSr Software EngineerCommented:

Write a code under the password textbox KeyDown event  and catch the key code using e.keychar , validate and invoke the login button click

Avinash Desai
Matb396Author Commented:
Mike TomlinsonMiddle School Assistant TeacherCommented:
Another approach is to set the AcceptButton() Property of the FORM to your login button.  This will cause it to be clicked when the user hits enter.  In the button click event you can check to see if one the fields is still empty and cause focus to return there.  If both fields are filled in then go ahead and attempt the login...
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AcceptButton = Me.btnLogon
    End Sub

    Private Sub btnLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogon.Click
        If tbUserName.Text.Trim.Length = 0 Then
        ElseIf tbPassword.Text.Trim.Length = 0 Then
            ' ... normal button login code here ...
        End If
    End Sub

End Class

Open in new window

If you are using Visual Studio, you just set the Default property of the Logon button to true and it is clicked on Enter.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.