. net主菜单帮助



我试图使一个主菜单接受用户输入,然后检查输入的密码对密码的有效性,我硬编码到一个数组。首先,在for循环中,只检查第一个密码索引。我希望根据ValidPasswords()数组中的每个密码检查输入的密码。

第二,我的for循环没有做我想让它做的事情。我想给用户3次机会输入密码…如果他/她超过3次,它会告诉他们已经尝试了3次,并退出表单。现在,它只是循环3次,然后退出,不给用户再次尝试的机会。如果我放一条return语句进去,它就会一直返回,不会循环3次。

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

谢谢!

丹尼尔,你把东西放回前面了。我不会给您提供关于应用程序中硬编码密码的建议,并假设您只是试图掌握基本知识……我也假定是。net 4,因为你没有指定;-)

我是手工做的,所以请原谅语法上的小问题:

Public Class frmMain    
    Private validPasswords As List(Of String) = New List(Of String) From {"1234", "2222", "8918", "9911"}    
    Private failedAttempts As Integer = 0    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
    If String.IsNullOrWhitespace(txtFNAME.Text) Then 
        MsgBox("Please enter a name") 
        Return
    End If
    If ValidPasswords.Any(Function(x) String.Equals(txtPW.Text, x)) Then
        ' User has a name and entered a valid password...
        g_welcomeMessage = ("Welcome, " + txtFNAME.Text + " " + txtLNAME.Text + ", to Image Viewer 1.0")
        frmImage.ShowDialog()
    Else
        failedAttempts += 1
        If failedAttempts = 3 Then
            MessageBox.Show("Exceeded 3 password attempts.")
            Me.Close()
        End If
    End If
End Sub
' The other method here...

结束类

最新更新