2011-07-05 161 views
0

我想做一個主菜單,接受用戶輸入,然後檢查輸入的密碼的有效性對密碼我硬編碼到一個數組。 首先,在for循環中,只檢查第一個密碼索引。我希望輸入的密碼在ValidPasswords()數組內的每個密碼上進行檢查。NET主菜單幫助

二,我的循環沒有做我想做的事情。我想給用戶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 

謝謝!

回答

2

你把事情回到那裏丹尼爾。我不會在你的應用程序中提供關於硬編碼密碼的建議,並假設你只是想掌握基本知識......我也會假設.Net 4,因爲你沒有指定;-)

I 「M手工做這個,所以藉口任何輕微的語法問題:

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... 

末級

+0

是的,我想我確實有它向後。我正在玩一堆不同的方式來寫它......但這真棒。希望有一天我能夠這樣編碼:)我想要做的不是硬編碼密碼......而是用戶註冊,將創建的密碼保存到數據庫/可調整大小的陣列,然後使用它。雖然可能太高級了。任何方向?謝謝! – Daniel

+0

絕對@Daniel - 我很高興你正在尋找知識,而不是堅持你所知道的 - 在我看來最好的學習方式。只是谷歌的VB.Net用戶登錄,有很多不同的方式來做到這一點的例子。我相信你甚至可以在應用程序中很容易地使用Asp.Net的會員提供商。當你的學習......分階段構建它時,就不要去尋找一個完整的董事會系統 - 將你的用戶詳細信息存儲在應用程序中 - 接下來是一個數據庫 - 接下來是一個完整的定製會員供應商。希望有所幫助! – Smudge202

+0

嗯,作爲一個後來的想法,如果你喜歡我的代碼風格,添加到你不斷增長的事物列表中來閱讀和學習:Lambda表達教程(_note指出多線Lambda與.Net 4!_一起引入)和還有Linq(特別是Linq to Entity/Linq to Object - 這個詞根據文章的年齡而不同)。快樂編碼 – Smudge202