While loop!! Plez help

futsack88

New member
Joined
Nov 21, 2006
Messages
1
Programming Experience
Beginner
i know this sound stupid however im using a while loop for a password enter type thing and i want so that if u enter the password 3 time wrong then the program will close. But all my loop wnt let me type in the password three time it only lets my do it once and then closing the program is there anything that i maby left out??

Dim user As String
Dim password As String
Dim counter As Short

password = "vangough"
user = "pp01"
counter = 0

While counter < 3
If txtuserid.Text = user Then
If txtpassword.Text = password Then
'Text Boxes (Password Page)'
txtuserid.Visible = False
txtpassword.Visible = False
'Buttons (Password Page)'
btnlogin.Visible = False
'Labels (Password Page)'
lbluserid.Visible = False
lblpassword.Visible = False
lbltitle.Visible = False
'Text Boxes (Main Page)'
'Room Details'
txtrheight.Visible = True
txtrwidth.Visible = True
txtrlength.Visible = True
'Window Details'
txtwheight.Visible = True
txtwwidth.Visible = True
'Door Details'
txtdheight.Visible = True
txtdwidth.Visible = True
'Buttons(Main Page)'
btnconfirm.Visible = True
'Labels (Main Page)'
'Room Details'
lblroom.Visible = True
lblrheight.Visible = True
lblrwidth.Visible = True
lblrlength.Visible = True
'Window Details'
lblwindow.Visible = True
lblwheight.Visible = True
lblwwidth.Visible = True
'Door Details'
lbldoor.Visible = True
lbldheight.Visible = True
lbldwidth.Visible = True
'Paint'
lblpaint.Visible = True
lblpainttype.Visible = True
'Picboxs (Main Page)'
'Paint'
picbxwhite.Visible = True
picbxmagnolia.Visible = True
picbxsun.Visible = True
picbxapple.Visible = True
picbxraspberry.Visible = True
picbxsea.Visible = True
'check boxes' (Main Page)
'Paint'
ckbxwhite.Visible = True
ckbxmagnolia.Visible = True
ckbxsun.Visible = True
ckbxapple.Visible = True
ckbxraspberry.Visible = True
ckbxsea.Visible = True
ckbxmatt.Visible = True
ckbxsilk.Visible = True
ckbxsatin.Visible = True
'Help Menu' (Main Page)
logout.Visible = True
clear.Visible = True

Else
counter = counter + 1
'Error message for incorrect password'
MsgBox("The PASSWORD you have entered seems to be incorrect")

End If
Else
counter = counter + 1
'Error message for incorrect userID'
MsgBox("Sorry this USER ID is not registered to this computer")
End If
End While
 
Where is this code placed? In a button click event handler?

I hope this is only a learning exercise as the design flawed. In a real app you would show a separate form to login, then show the main form if the login succeeds.

To get your example working though, you need to declare a class variable (counter), outside of any methods/procedures, setting its value to zero initially. Then after the user enters the username/password and clicks a button, check for validity and if not valid, increment the class variable by one. If the variable equals three then don't allow anymore login attempts.
Psuedo code:
VB.NET:
Private counter as Integer = 0

Private Sub cmdLogin_Click(...) Handles cmdLogin.Click
    If txtuserid.Text = user AndAlso txtpassword.Text = password Then
        'success, show the form (or controls in your case)
    Else
        MessageBox.Show("The PASSWORD you have entered seems to be incorrect")
        counter += 1
        If counter = 3 then
            'disable the login button (or do something else)
            cmdLogin.Enabled = False
        End if
    End if
End Sub
 
You also need to prompt for userID and password input at the end of the loop. If you don't (as in your current code) the loop will execute again with the same values. So, it is only allowing 3 attempts, but you don't let the user put in different values.
 
Back
Top