.NET Main Menu Help

Growler11

New member
Joined
Jul 5, 2011
Messages
1
Programming Experience
Beginner
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.

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:
this is because you cannot putsomething like this in a loop. I learned this lesson a while ago. When aking for something, never put it in a loop. set a counter variable and on the click use an if statement to validate the input. check the counter variable to see it it is 3 yet, if they have had 3 tries do what you want to do. if not check if the input is correct then let the user through. If it is not correct add 1 to the counter. the next time the user clicks the button it will be validated again, until the counter variable has exceeded 3.

here is just some pseudo

Dim counter As Integer = 0


if counter >3 then

end


else
If txtPW.Text = whatever then
let user through
else counter +=1



thats eally all you need any more questions ask me.
 
Back
Top