Password Enter

Hi all

I have made a vb.net 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.

Matt
Matb396Asked:
Who is Participating?
 
x77Commented:
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
         .....
0
 
Avinash DesaiSr Software EngineerCommented:
Hi

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

Regards
Avinash Desai
0
 
Matb396Author Commented:
Thanks
0
 
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
            tbUserName.Focus()
        ElseIf tbPassword.Text.Trim.Length = 0 Then
            tbPassword.Focus()
        Else
            ' ... normal button login code here ...
            MessageBox.Show("Logon")
        End If
    End Sub

End Class

Open in new window

0
 
TommySzalapskiCommented:
If you are using Visual Studio, you just set the Default property of the Logon button to true and it is clicked on Enter.
0
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.