Question For Loop for Login Form

kieran82

Member
Joined
Feb 21, 2010
Messages
19
Programming Experience
Beginner
i am trying to do a for loop to give the user 3 trys to get the login username and password right. the username is User and the Password is VB. the code i got so far is a if function
VB.NET:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'Stores MainForm in memory as New Form
        Dim MainForm As New frmCustomerDetails
        Dim strUsername As String = "User"
        Dim Password As String = "VB"

        strUsername = txtUsername.Text
        Password = txtPassword.Text

        For counter As Integer = 1 To 3
               If strUsername = "User" And Password = "VB" Then
                Username = txtUsername.Text
                frmLoginForm.ActiveForm.Hide()
                MainForm.Show()
                MessageBox.Show("Login Success", "Login")
                Exit For
            Else
                'if Login Failed Popup message Box and clear textboxes for retry
                MessageBox.Show("Login Failed, Please Try Again", "Login")
                counter += 1
                txtUsername.Clear()
                txtPassword.Clear()
                txtUsername.Focus()
                End
            End If
        Next counter
    End Sub
 
i am trying to do a for loop to give the user 3 trys to get the login username and password right. the username is User and the Password is VB. the code i got so far is a if function
VB.NET:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'Stores MainForm in memory as New Form
        Dim MainForm As New frmCustomerDetails
        Dim strUsername As String = "User"
        Dim Password As String = "VB"

        strUsername = txtUsername.Text
        Password = txtPassword.Text

        For counter As Integer = 1 To 3
               If strUsername = "User" And Password = "VB" Then
                Username = txtUsername.Text
                frmLoginForm.ActiveForm.Hide()
                MainForm.Show()
                MessageBox.Show("Login Success", "Login")
                Exit For
            Else
                'if Login Failed Popup message Box and clear textboxes for retry
                MessageBox.Show("Login Failed, Please Try Again", "Login")
                [B][COLOR="red"]counter += 1[/COLOR][/B] 'why add one to the counter? VB will do this when it goes round the loop
                [COLOR="red"][B]txtUsername.Clear()[/B][/COLOR] ' typically we don't clear the username the user has typed
                txtPassword.Clear()
                txtUsername.Focus()
                End
            End If
        Next counter
    End Sub

You didnt actually ask a question in your post, so I guessed what it might be. If you need further advice, please remember to ask a question :)
 
Why are you using a for loop? Why not just increment the counter each time it fails and once it gets to 3 take the required action?
 
Back
Top