Results 1 to 3 of 3

Thread: For Loop for Login Form

  1. #1
    kieran82 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Feb 2010
    Posts
    19
    Reputation
    42

    For Loop for Login Form

    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
    Code:
    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

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1705
    Quote Originally Posted by kieran82 View Post
    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
    Code:
    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 'why add one to the counter? VB will do this when it goes round the loop
                    txtUsername.Clear() ' 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

  3. #3
    LeonR is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2006
    Location
    UK
    Posts
    141
    Reputation
    84
    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking