14 Replies - 7221 Views - Last Post: 04 July 2012 - 08:07 AM Rate Topic: -----

#1 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Right Syntax for Looping

Posted 04 July 2012 - 01:33 AM

hi guys, im new to vb.net and i would like to ask if someone cam teach me how to make this ode right.
Public Class FrmSecurityProgram



    Private Sub BtnOK_Click(sender As System.Object, e As System.EventArgs) Handles BtnOK.Click
        Dim ans As String
        'Dim ctr1 As Integer
        'While ctr1 <= 3
        If Txtusername.Text = "julie" And Txtpassword.Text = "mamatra" Then
            MsgBox("Access Granted!")
            Me.Hide()
            FrmSimpleMath.ShowDialog()

        Else
            ans = MsgBox("Access Denied! Try Again?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical)
            If ans = MsgBoxResult.Yes Then
                Txtpassword.Clear()
                Txtusername.Clear()
                Txtusername.Focus()

            End If
        End If
        'End While

    End Sub

  
End Class


it is a simple security program where it will ask for username and password. the user just have 3 times to try and when its the fourth try the msgbox will show and will tell the user that he exceeds the maximum number of try.

im still a student now and i really want to know how this could work thank you in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Right Syntax for Looping

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Right Syntax for Looping

Posted 04 July 2012 - 02:00 AM

hi,
you almost got the code working right. you just need to increment the "ctr1" variable every time the user enter incorrect pass or username. also i suggest you in the line where you check if the result of the dialog is yes(If ans = MsgBoxResult.Yes Then) to put an else and if the result is no to exit the loop using Exit While.
good luck :)

This post has been edited by raziel_: 04 July 2012 - 02:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Right Syntax for Looping

Posted 04 July 2012 - 02:35 AM

This is not really important but when the user inputs their username / password the way your code is at the moment it will be case senstive you may want it this way which is far enough but if you don't want it case senstive look into toupper or tolower
Was This Post Helpful? 2
  • +
  • -

#4 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 03:10 AM

View Postraziel_, on 04 July 2012 - 02:00 AM, said:

hi,
you almost got the code working right. you just need to increment the "ctr1" variable every time the user enter incorrect pass or username. also i suggest you in the line where you check if the result of the dialog is yes(If ans = MsgBoxResult.Yes Then) to put an else and if the result is no to exit the loop using Exit While.
good luck :)


thank you! :)

View PostLieannM, on 04 July 2012 - 03:06 AM, said:

View Postraziel_, on 04 July 2012 - 02:00 AM, said:

hi,
you almost got the code working right. you just need to increment the "ctr1" variable every time the user enter incorrect pass or username. also i suggest you in the line where you check if the result of the dialog is yes(If ans = MsgBoxResult.Yes Then) to put an else and if the result is no to exit the loop using Exit While.
good luck :)


thank you! :)

another thing, what is the syntax for increment here in vb.net? :)
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Right Syntax for Looping

Posted 04 July 2012 - 03:30 AM

hi,
about how to increment
Dim i As Integer
i=i+1
'or
i+=1



As m_wylie85 sad you should think of making the user name case insensitive you can use .ToLower method. and you should think of using DB for your login. If your program is simple you can at least use a file that you can save the passwords and username. this way you can make a login form that you can create a users. try avoiding using hard coded passwords and user names. you can only add some admin pass or something that can be used by you only.
good luck :)

This post has been edited by raziel_: 04 July 2012 - 03:32 AM

Was This Post Helpful? 1
  • +
  • -

#6 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 03:40 AM

View Postraziel_, on 04 July 2012 - 03:30 AM, said:

hi,
about how to increment
Dim i As Integer
i=i+1
'or
i+=1



As m_wylie85 sad you should think of making the user name case insensitive you can use .ToLower method. and you should think of using DB for your login. If your program is simple you can at least use a file that you can save the passwords and username. this way you can make a login form that you can create a users. try avoiding using hard coded passwords and user names. you can only add some admin pass or something that can be used by you only.
good luck :)


thank you! :)
Was This Post Helpful? 0
  • +
  • -

#7 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 04:06 AM

Your code is fine,except a few things:
1. you should declare "ans" as MsgBoxResult not as string.
2. you should declare ctr1 in the scope of class FrmSecurityProgram and then you can use While loop
3. when the password is wrong,increase ctr1
Was This Post Helpful? 1
  • +
  • -

#8 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 04:57 AM

 Dim ctr As Integer
        ctr = 3

        While ctr = 3
            If TxtUsername.Text = "julie" And TxtPassword.Text = "mamatra" Then
                MsgBox("Access Granted!")
                Me.Hide()
                FrmSimpleMath.ShowDialog()

            Else

                MsgBox("Access Denied! Try Again?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical)
                If MsgBoxResult.Yes Then
                    ctr -= 1

                    If ctr = 0 Then
                        MsgBox("Error!", MsgBoxStyle.Critical)
                    Else
                        TxtPassword.Clear()
                        TxtUsername.Clear()
                        TxtUsername.Focus()

                    End If

                Else
                    End
                End If
            End If
        End While
 



hi guys, im sorry :( i cant think correctly right now. i need to loop this 3 times (3 tries). and for the 4th try the program will show error msg and will end. :( how is it? :(
Was This Post Helpful? 0
  • +
  • -

#9 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Right Syntax for Looping

Posted 04 July 2012 - 05:08 AM

your logic is incorrect. why your loop is till "ctr" is equal to 3 and then you decrease it:
       While ctr = 3


and then

If MsgBoxResult.Yes Then
                   ctr -= 1



also ctr should be a class scope, global for your class/form. another thing you should increase "ctr" right after the else where is your logic for incorrect result:
           Else
               'here you should increment ctr'
               MsgBox("Access Denied! Try Again?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical)


then you should check if the ctr is equal to 3 show error that they dont have more tries and exit the wile this way the question about do they want to try again will not show. then after your loop check again for the ctr if its 3 and if so exit the sub, end the program or do what ever you want to do.

This post has been edited by raziel_: 04 July 2012 - 05:09 AM

Was This Post Helpful? 1
  • +
  • -

#10 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 05:27 AM

[quote name='raziel_' date='04 July 2012 - 05:08 AM' timestamp='1341403718' post='1657253']
your logic is incorrect. why your loop is till "ctr" is equal to 3 and then you decrease it:
       While ctr = 3


what must i put on my while ? :)
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Right Syntax for Looping

Posted 04 July 2012 - 05:56 AM

Programming is just writing code into an IDE, the vast majority of it is problem solving.

What do want the code do?
What is the code actually doing?
What happens when the login is successful? unsuccessful?

If MsgBoxResult.Yes Then



Isn't working as you think it is, because you don't capture the result of the messagebox.

Debugging Tutorial
Was This Post Helpful? 0
  • +
  • -

#12 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 06:15 AM

View PostAdamSpeight2008, on 04 July 2012 - 05:56 AM, said:

Programming is just writing code into an IDE, the vast majority of it is problem solving.

What do want the code do?
What is the code actually doing?
What happens when the login is successful? unsuccessful?

If MsgBoxResult.Yes Then



Isn't working as you think it is, because you don't capture the result of the messagebox.

Debugging Tutorial


then what am i supposed to do? :(
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Right Syntax for Looping

Posted 04 July 2012 - 06:24 AM

Think. Do Research. Experiment.

Research: While Loop
Research: MessageBox

etc

If you struggle take the problem down to the basics.
Create a simple console applications, to test out ideas and concepts without added complication a UI framework brings.
Was This Post Helpful? 0
  • +
  • -

#14 LieannM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-June 11

Re: Right Syntax for Looping

Posted 04 July 2012 - 06:36 AM

View PostAdamSpeight2008, on 04 July 2012 - 06:24 AM, said:

Think. Do Research. Experiment.

Research: While Loop
Research: MessageBox

etc

If you struggle take the problem down to the basics.
Create a simple console applications, to test out ideas and concepts without added complication a UI framework brings.


thanks a lot :)
Was This Post Helpful? 0
  • +
  • -

#15 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Right Syntax for Looping

Posted 04 July 2012 - 08:07 AM

when your done with your research and when you give it a try you are welcome to post your code if you have more troubles.
good luck :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1