Looping about simple log-in form

giving the user to log-in correctly the password 3 times only

Page 1 of 1

14 Replies - 19288 Views - Last Post: 18 February 2010 - 11:49 AM Rate Topic: -----

#1 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Looping about simple log-in form

Posted 17 February 2010 - 09:14 AM

Public Class Form1

Private Sub cmdLogIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogIN.Click
Dim count As Integer
count = 0


Do Until (count = 3)
Label1.Text = count
If txtpass.Text = "admin" Then
MsgBox("thank you")

Exit Sub
Else
MsgBox("try again")
count = count + 1
End If
Loop
End Sub
End Class


so this is the problem our professor gave us. the user given a chance to log-in correctly 3 times, assuming that a user log-in his/her password incorrectly there will be a loop, counting its no. of attempts. and if it exceeds to 3, there will be a msgbox assuming "you log-in incorrectly 3 times" etc. here in my code it counts but the msgbox ("try again") shows 3 times. i think i really need your help, not to give the right code but to show me the light, explain to me, give me an idea so that i can figure out by myself. thanks a lot.

btw, dont mind the design/objects. for now i just want to figure out the logic.

Is This A Good Question/Topic? 0
  • +

Replies To: Looping about simple log-in form

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Looping about simple log-in form

Posted 17 February 2010 - 09:30 AM

First, even for short code fragments like yours, please use the code tags around your code:

:code:



Your counter will get reinitialized to zero each time the user clicks the button.

So, the counter needs to be declared outside the method.

In your code you are looping on the same txtpass.Text 3 times.

What you want to do is when the button is clicked is

  • check the password
  • if it is correct, print the success message
  • reset the counter to zero for the next user and exit the method
  • if the password is incorrect, print the retry message
  • increment the counter
  • and exit the method


How you handle 3 failed attempts is left for later :)

This post has been edited by n8wxs: 17 February 2010 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:14 AM

Public Class Form1

    Private Sub cmdLogIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogIN.Click
        Dim count As Integer
        count = 0
        If txtpass.Text = "admin" Then
            MsgBox("thank you")
            Exit Sub
        End If

        Do Until (count = 3)
            Label1.Text = count
            If txtpass.Text <> "admin" Then
                MsgBox("try again")
            End If
            count = count + 1
        Loop
    End Sub
End Class


i come up with this. thanks for the guidelines.this is what i cant figure out

Quote

# if the password is incorrect, print the retry message
# increment the counter
# and exit the method
everytime the answer is false, the msgbox "try again" comes up times. where should i put the

Quote

count = count + 1
???
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:23 AM

I'm using // as a comment delimiter so the code window stays colorful :)
Public Class Form1
        // count global to event
        Dim count As Integer = 0

    Private Sub cmdLogIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogIN.Click
//      Dim count As Integer
//      count = 0

        If txtpass.Text = "admin" Then
            MsgBox("thank you")

            count = 0 // reset counter for  next time
            Exit Sub
        End If

        If txtpass.Text <> "admin" Then
            MsgBox("try again")

        End If

        count = count + 1

        // you do  not want to loop here because the user cannot change
        // the password.

//        Do Until (count = 3)
//            Label1.Text = count
//           If txtpass.Text <> "admin" Then
//                MsgBox("try again")
//            End If
//            count = count + 1
//        Loop

    End Sub
End Class



What you do when the count equals 3 (or whatever limit of tries the user gets)
still has to be fleshed out. If you want to skip the event method when the limit is reached this will do it:

    Private Sub cmdLogIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogIN.Click

        if count = 3 then
            exit sub

        End If

        If txtpass.Text = "admin" Then
            MsgBox("thank you")

            count = 0 // reset counter for  next time
            Exit Sub
        End If
...


This post has been edited by n8wxs: 17 February 2010 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:35 AM

...wait..

This post has been edited by mpe-IT: 17 February 2010 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:43 AM

Quote

// you do not want to loop here because the user cannot change
// the password.

// Do Until (count = 3)
// Label1.Text = count
// If txtpass.Text <> "admin" Then
// MsgBox("try again")
// End If
// count = count + 1
// Loop


i cant understand this one
Was This Post Helpful? 0
  • +
  • -

#7 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:50 AM

i just cant figure out where to put the incrementation. in that part, it disable me to enter another password. when i click the button which is not equal to "admin" a msgbox pop-up then when i click okay another msgbox comes total of 3 msgbox with try again note.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:54 AM

Think of it this way.
You are clicking the button after the user enters their password. If you loop inside that button, you are not allowing the user to make any changes before your code tries 3 times.

What n8wxs is showing is each button click is being processed much like a single iteration of a loop. However, by incrementing the counter each unsuccessful click, that loop only runs 3 times.

This post has been edited by CharlieMay: 17 February 2010 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Looping about simple log-in form

Posted 17 February 2010 - 11:58 AM

Did you try the code i posted before
Was This Post Helpful? 0
  • +
  • -

#10 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 07:43 PM

yes sir, i've tried it. same result, msgbox shows 3 times
Was This Post Helpful? 0
  • +
  • -

#11 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 17 February 2010 - 07:48 PM

your code is working, but our prof told us to use a loop.
Was This Post Helpful? 0
  • +
  • -

#12 camckee316   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 292
  • Joined: 29-August 08

Re: Looping about simple log-in form

Posted 17 February 2010 - 08:46 PM

Would something like this suit your needs?
      
Dim count As Integer=0
        


        If txtpass.Text = "admin" Then
            MessageBox.Show("thank you", "login successfull", MessageBoxButtons.OK)
            Exit Sub

        Else
            count += 1
        End If

        If count <= 3 Then
            MessageBox.Show("try again", "login failed", MessageBoxButtons.OK)
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#13 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 18 February 2010 - 08:54 AM

thanks for all your help. actually, that's what exactly i did(code above me). i defend it to my prof. i said, what important is that i've provide solution to the problem. that's what matters right, pals? but for the sake of learning she said i must do it using a loop statement. when she said that my mind says why is this proof make things complicated where in i had a solution already.
but somehow she has point. i/we must learn all the things especially loopings. she told me she will reveal her code in our next meeting. so what i can do now is to wait. thank you guys..

n8wxs
camckee316
CharlieMay

thanks for being nice to me although lets say i'm a beginner. in other forums, some people are so boastful
Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Looping about simple log-in form

Posted 18 February 2010 - 09:02 AM

To do what your professor is wanting to do using a loop would seem to me to require an InputBox to break the execution for User Input. This however has 2 issues. The Inputbox will probably not be supported in future versions as it is code passed down to help convert vb6 code to .net. And 2, you are very limited to what you can even do with it as far has things like Hiding what the user types (crucial for passwords) limiting what the user can enter etc...

EDIT:
You might post the code when you get it. I'd like to see her version of this

This post has been edited by CharlieMay: 18 February 2010 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#15 mpe-IT   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Looping about simple log-in form

Posted 18 February 2010 - 11:49 AM

yeah i'll post it as soon i see her code maybe next week. but i doubt its impossible..idk..maybe..actually im excited to see her code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1