Hi,
I'm trying to make a main menu which accepts user input and then checks inputted password for validity against passwords I hardcoded into an array.
Firstly, in the for loop, only the first password index is being checked. I'd like the inputted password to be checked against EACH password inside the ValidPasswords() array.
Second, My for loop isn't doing what I want it to do. I'd like to give the user 3 chances to enter a password... If he/she exceeds 3, it tells them they've tried 3 times and exits the form. Right now, it just loops 3 times and exits without giving the user a chance to try again. If I put a return statement in, it just keeps returning and doesn't loop 3 times.
Thank you!!
I'm trying to make a main menu which accepts user input and then checks inputted password for validity against passwords I hardcoded into an array.
Firstly, in the for loop, only the first password index is being checked. I'd like the inputted password to be checked against EACH password inside the ValidPasswords() array.
Second, My for loop isn't doing what I want it to do. I'd like to give the user 3 chances to enter a password... If he/she exceeds 3, it tells them they've tried 3 times and exits the form. Right now, it just loops 3 times and exits without giving the user a chance to try again. If I put a return statement in, it just keeps returning and doesn't loop 3 times.
VB.NET:
Public Class frmMain
Dim ValidPasswords() = {"1234", "2222", "8918", "9911"}
'Dim ValidPWList As New List(Of String)
Dim pwIndex As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' For pwIndex = 0 To ValidPasswords.Length 'TOTAL PASSWORDS
If txtPW.Text = ValidPasswords(pwIndex) Then
Else
For i = 0 To 2 '3 MAX ALLOWABLE ATTEMPT
MessageBox.Show("Invalid Password, Please try again.", "Invalid Credentials")
txtPW.Focus()
Next
MessageBox.Show("Exceeded 3 password attempts.")
Me.Close()
End If
If txtFNAME.Text = "" Then
MessageBox.Show("Please enter your name!", "Error")
'ElseIf txtPW.Text <> "1234" And txtPW.Text <> "2332" And txtPW.Text <> "0192" And txtPW.Text <> "2010" Then
'MessageBox.Show("Invalid Password, Please try again.", "Invalid Credentials")
Else
g_welcomeMessage = ("Welcome, " + txtFNAME.Text + " " + txtLNAME.Text + ", to Image Viewer 1.0")
frmImage.ShowDialog()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show("Thanks for trying me out!", "Goodbye")
Me.Close()
End Sub
End Class
Thank you!!
Last edited: