2013-11-04 20 views
0

我想爲登錄功能添加登錄嘗試次數。當用戶輸入錯誤的用戶名和密碼3次時,程序應該關閉並顯示一條消息。這裏是我的登錄按鈕代碼在我Form1.vb如何對登錄嘗試進行計數Visual Basic

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or 
     TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or 
     TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or 
     TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or 
     TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or 
     TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or 
     TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or 
     TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or 
     TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or 
     TextBox1.Text = "1" And TextBox2.Text = "1" Or 
     TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then 
     Timer1.Start() 'Timer on Form1.vb show 
     ProgressBar1.Show() 'Progress bar on Form1.vb show 
     Label8.Show() 'Label8 on Form1.vb show 
     Button4.Show() 'Button4 on Form1.vb show 

    Else 
     If TextBox1.Text = "" And TextBox2.Text = "" Then 
      MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field 
     Else 
      If TextBox1.Text = "" Then 
       MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found 
      Else 
       If TextBox2.Text = "" Then 
        MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found 
       Else 
        MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password 
        TextBox2.Clear() 
       End If 
      End If 
     End If 
    End If 

End Sub 

我能做些什麼,以添加計數到該代碼正常通知他們3次失敗的登錄嘗試的用戶?

+0

'私人NCOUNT爲整數= 0'然後每次失敗'時間NCOUNT = 1'然後退出應用程序時,它會以3 – Plutonix

+0

我肯定有人在那裏讚賞你的廣播你的用戶名和密碼... :) – cHao

+0

感謝您的幫助,非常感謝 –

回答

0

正如一些人所建議的,你可以創建一個變量(cntAttempts),用於跟蹤用戶嘗試登錄多少次,如果計數達到3,則程序關閉。就像這樣:

Private cntAttempts = 0 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    If ... Then 
     Timer1.Start() 'Timer on Form1.vb show 
     ProgressBar1.Show() 'Progress bar on Form1.vb show 
     Label8.Show() 'Label8 on Form1.vb show 
     Button4.Show() 'Button4 on Form1.vb show 

    Else 
     cntAttempts += 1 
     If cntAttempts = 3 Then 
      MessageBox.Show("login failed") 
      Me.close() 
     End If 
     If TextBox1.Text = "" And TextBox2.Text = "" Then 
      ... 
     Else 
      ... 
     End If 
    End If 

End Sub 

HTH

+0

感謝您的幫助我現在已經開始工作了 –

0

我添加了cnt作爲整數並增加它直到cnt < = 3。

私人小組的button1_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手Button1.Click

'Dim cnt As Integer = 0 
    If cnt <= 3 Then 
     If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or 
      TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or 
      TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or 
      TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or 
      TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or 
      TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or 
      TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or 
      TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or 
      TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or 
      TextBox1.Text = "1" And TextBox2.Text = "1" Or 
      TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then 
      Timer1.Start() 'Timer on Form1.vb show 
      ProgressBar1.Show() 'Progress bar on Form1.vb show 
      Label8.Show() 'Label8 on Form1.vb show 
      Button4.Show() 'Button4 on Form1.vb show 
     Else 
      cnt = cnt + 1 
      If TextBox1.Text = "" And TextBox2.Text = "" Then 
       MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field 
      Else 
       If TextBox1.Text = "" Then 
        MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found 
       Else 
        If TextBox2.Text = "" Then 
         MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found 
        Else 
         MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password 
         TextBox2.Clear() 
        End If 
       End If 
      End If 
     End If 
    End If 
End Sub` 
+0

對不起,這不起作用,它也沒有幫助關閉程序,一旦櫃檯達到3,如果你可以進一步幫助我將不勝感激 –

+0

我已經得到它的工作,現在感謝你和答案以上再次感謝 –

0
Dim a =0 

    If txtname.Text = "yourUsername" And txtpass.Text = "yourPassword" Then 
    Msgbox("Acces Granted") 
    a=0 
    Elseif txtname.Text <> "yourUsername" Or txtpass.Text <> "yourPassword" Then 
    a = MsgBox("INVALID USERNAME AND PASSWORD") + a 

    End if 

    If 
    a = 3 Then 
    End 

    End if 
+0

當您需要的是一個整數時,您聲明'a'爲對象。您還將枚舉的值('DialogResult')添加到'a'並假定它總是一個。 – JoelC

+0

請幫助我這個[http://stackoverflow.com/a/26203112/3902343] –

1
Public Class Form1  
    Dim attempts As Integer = 0 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim user As String 
     user = TextBox1.Text 
     If user = "Jhayboy" Then 
      MsgBox("Access Granted") 
      Form2.Show() 
      Me.Hide() 

     ElseIf attempts = 3 Then 
      MsgBox("Maximum count of retries(3),And you'reach the maximum attempts!Try again later", MsgBoxStyle.Critical, "Warning") 
      Close() 
     Else 
      MsgBox("Username and Password is incorrect! re-enter again you currently have reached attempt " & attempts & " of 3.") 

      attempts = attempts + 1 
      TextBox1.Text = "" 
      TextBox1.Focus() 

     End If 
    End Sub 
End Class