Results 1 to 3 of 3

Thread: Enter Password before running the code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    71

    Unhappy Enter Password before running the code

    Hi All,

    I am new to VB NET and I have tried to make a short code to ask the user to enter the password up to 3 times. If 3 attemps are given invalid password, then close/stop the code [main()] from running completely and unload the form.

    Code:
    Public Class Form1
        Private Sub START_PRINT_CAPTURES_Click(sender As Object, e As EventArgs) Handles START_PRINT_CAPTURES.Click
    
            Dim password As String = "123456xxxx"
            Dim pw As String = InputBox("Please enter password to continue: ")
    
            If pw = password Then
                Main() '// Run main code
            Else
                MsgBox("Input is invalid. Please try again")
                End
            End If
    
        End Sub
    End Class
    Obviously, my code does not work.

    Please correct me. Thank you in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Enter Password before running the code

    What's the actual point of this? Are you requiring the user to enter a password to run your app or are you requiring a password to access a specific feature in your app?

    Apart from anything else, it should be obvious that if you want to do something multiple times then you use a loop to do so. If you have learned about loops then it shouldn't be hard to write one that will run a maximum of three times but will exit if a specific condition is met.

  3. #3
    Member
    Join Date
    Apr 2011
    Posts
    35

    Re: Enter Password before running the code

    You can find many tutorials about this online, maybe try to follow what i say? I will just talk as if im explaining i wont give you code, this way you can try to work it out?

    Declare both the pass and failattempts at the top (right under Public Class Form1)

    password = password (string)
    failAttempt = 0 (integer)
    Code:
    if failAttempt < 3 then
        if userinput = password then
            display a message or open a new form
            reset failAttempt to default value declared at the top
       elseif userinput = "" then
            display a message telling user to enter something into the password box
       elseif userinput IsNot password then
            display a message telling them it's the wrong pass
            failAttempt = failAttempt + 1
       end if
    else
       display a message telling the user they are locked out for 3 failed attempts
    end if

    The above works and the if statements are really simple to understand and use and from there you should then move on to loop's and make the code more efficient.

    Read through the above and try to put it into code. Simple and easy.

Posting Permissions

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



Click Here to Expand Forum to Full Width