Search code examples
vb.netauthenticationtextbox

Login with textboxes in visual basic, 3 tries


I'm making a login script in VB using textboxes. My problem is that the msgbox which inform the user about attempts left keep looping itself as well and using up all the (3) tries. Not sure what's wrong.

here is my code:

Dim cor_pw As String = "1234"
Dim cor_us As String = "abcd"
Dim tries1 As Integer = 3

tries1 -= 1

Do
    MsgBox("wrong combination, " & tries1 & " chances left to make it right.")

Loop Until textbox1.Text = cor_us And textbox2.Text = cor_pw Or tries1<= 0

If textbox1.Text = cor_us And textbox2.Text = cor_pw Then
    MsgBox("Congratulations! That's Correct!")
Else
    MsgBox("You've used all tries! " & tries1 & " chances left, good bye!")
    Me.Close()
End If

Solution

  • You need an OK button that indicates the user has finished entering text. In the event for the OK button you would

    validate the text
    If valid then you are good
    otherwise increment the retry1 variable -- which must be declared at the Form (module) level!
    now test retry1
    if retry 1 is > 3 then display failure message and disable the OK button
    otherwise display retry message 
    exit the sub
    

    At this point the user can reenter the values and then hit OK button again. No loop.