Public Class frmLogin

  'frmLogin's code
  Public userName As String
  Dim numTries As Integer = 0
  Dim idVerified As Boolean = False

  Private Sub btnContinue_Click(ByVal sender As System.Object,
                                ByVal e As System.EventArgs) Handles btnContinue.Click
    If (txtUserName.Text = "") Or (txtPassword.Text = "") Then
      MessageBox.Show("You must enter both a user name and a password.")
    Else
      If Confirm(txtUserName.Text, txtPassword.Text) Then
        idVerified = True
        userName = txtUserName.Text
        Me.Close()
      Else
        MessageBox.Show("Improper user name or password.")
        txtUserName.Clear()
        txtPassword.Clear()
        txtUserName.Focus()
      End If
    End If
    numTries += 1
    If (numTries = 3) And (Not idVerified) Then
      MessageBox.Show("This program is being terminated.")
      frmOrder.Close()
      Me.Close()
    End If
  End Sub

  Function Confirm(ByVal userName As String,
                   ByVal password As String) As Boolean
    Dim query = From line In IO.File.ReadAllLines("MASTERFILE.TXT")
                Where line = userName & "_" & password
                Select line
    If query.Count = 1 Then
      Return True
    Else
      Return False
    End If
  End Function

  Private Sub frmLOGIN_FormClosing(ByVal sender As Object,
                                   ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles _
                                   Me.FormClosing
    If Not idVerified Then
      MessageBox.Show("This program is being terminated.")
      frmOrder.Close()
    End If
  End Sub

End Class
