Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27400

VS 2010 How to do two password attempts to open up another form in Visual Basic?

$
0
0
I'm creating a program that allows users to see movie information and play movie trailers. These movie trailers are password protected, so I have to read passwords from a text file called passwords.txt, which I've already done, and check to see if the password entered by a user is one of the acceptable passwords.

When the password form displays, I type in a password and when the first incorrect password is entered, my program should delete the circles and return focus to the TextBox for the second password entry. The problem that I'm having is that only the second password entry messagebox appears and not the first one. I don't know why this is happening.

Here's my code:

Option Explicit On
Imports System.IO
Imports System.Runtime.CompilerServices
Public Class frmPassword
Public Passwords(9) As String
Public noOfPasswords As Int32
Public tryCount As Int32

Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
Dim flag1 As Boolean = False
Dim num2 As Integer = (noOfPasswords - 1)
Dim num1 As Integer = 0
Do While (num1 <= num2)
If (Passwords(num1) = txtPassword.Text) Then
flag1 = True
End If
num1 = (num1 + 1)

Loop
If (flag1) Then
frmMovies.Show()
Me.Hide()
Else
tryCount = (tryCount + 1)
If (tryCount = True) Then
MsgBox("Retype your password. You have one try left!", MsgBoxStyle.OkOnly, "Password Incorrect") ----------> FOCUS ON THIS!
txtPassword.SelectAll()
txtPassword.Text = ""
txtPassword.Select()
Else
MsgBox("You have typed the password incorrectly two times. The program will terminate!", MsgBoxStyle.OkOnly, "Shutting Down")
Me.Close()
End If
End If
End Sub

Private Sub frmPassword_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
noOfPasswords = 0
Dim StreamReader As StreamReader = File.OpenText("passwords.txt")
Do While (StreamReader.Peek() <> -1)
Passwords(noOfPasswords) = StreamReader.ReadLine()
noOfPasswords = (noOfPasswords + 1)

Loop
StreamReader.Close()
tryCount = 0

End Sub

Private Sub txtPassword_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress
If (Strings.Asc(e.KeyChar) = 13) Then
cmdOK_Click(RuntimeHelpers.GetObjectValue(sender), e)
End If
End Sub
End Class

Here's my password form:
Name:  PasswordForm.JPG
Views: 12
Size:  14.6 KB

In this picture, this messagebox doesn't show up the first time I type in the wrong password; This picture is what I want resolved!:
Name:  Password.JPG
Views: 12
Size:  14.9 KB

In this picture, when I type in the wrong password the first time, it goes straight to this messagebox:
Name:  Password2.JPG
Views: 12
Size:  19.1 KB
Attached Images
   

Viewing all articles
Browse latest Browse all 27400


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>