Password Validation Using Visual Basic

The program allows a user login up to three (3) times. After three unsuccessful attempts, the program exits.

Properties of the Login Form:

Login button
Name: cmdValid
Clear button
Name: cmdClear
Cancel button
Name: cmdExit
Textbox
Name: txtPassword Tag: [Enter the password as the value of this property] PasswordChar: *
Label (at the bottom)
Name: lblNotify Name: "You only have 3 login attempts"

The code:

  1. Public Class frmPassword
  2.     Dim ErrorCount As Integer
  3.  
  4.     Private Sub frmPassword_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         ErrorCount = 0
  6.     End Sub
  7.  
  8.     Private Sub cmdExit_Click(sender As System.Object, e As System.EventArgs) Handles cmdExit.Click
  9.         Me.Close() 'Handles manual exit from the program by the user
  10.     End Sub
  11.  
  12.     Private Sub cmdValid_Click(sender As System.Object, e As System.EventArgs) Handles cmdValid.Click
  13.  
  14.         'Checks to see if the value entered in the textbox field is the same as that saved in the 'tag property
  15.         If txtPassword.Text = txtPassword.Tag Then
  16.             MessageBox.Show("You have logged in successfully", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'If the password entered matches the 'tag property, a message box will be displayed to the user informing them that the login 'was successful.
  17.             txtPassword.Clear() 'Clears the characters in the textbox
  18.         Else
  19.             'Everytime login is not successful, the error count is incremented by 1
  20.             ErrorCount = ErrorCount + 1
  21.  
  22.             'Allows the user have another attempt at login
  23.             MessageBox.Show("Incorrect password", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
  24.  
  25.             txtPassword.Clear()
  26.  
  27.             'Positions the cursor in the textbox
  28.             txtPassword.Focus()
  29.         End If
  30.  
  31.         'If login was not successful at the first time, the value of the lblNotify is changed to 'alert the user that they have two more attempts left
  32.         If (ErrorCount = 1) Then
  33.             lblNotify.Text() = "You have 2 login attempts left"
  34.  
  35.             'If login was not successful for the second time, the value of the lblNotify is changed to alert the user that they have one more attempts left
  36.  
  37.         ElseIf (ErrorCount = 2) Then
  38.             lblNotify.Text() = "You have 1 login attempt left"
  39.  
  40.             'If login was not successful for the third time, a message box appears alerting the user 'that they have no more login attempts left
  41.         ElseIf (ErrorCount = 3) Then
  42.             MessageBox.Show(" You have exceeded the maximum login attempts. System is now exiting. ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
  43.  
  44.             'The system will then exit after the message box is closed
  45.             Application.Exit()
  46.         End If
  47.     End Sub
  48.  
  49.     Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
  50.         txtPassword.Clear() 'Clears the characters in the textbox field in case the user 'realizes they made a typo while typing in the password before clicking on the login 'button.
  51.     End Sub
  52. End Class

Comments

Nakakainis ayaw magrun sa MSVisual Basic 2010

Add new comment