Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO 'imports input and output form
- Public Class LoginData 'login data form
- Dim lineCount As Integer 'The full amount of records in the file
- Dim rowIndex As Integer 'The currently selected row of the datagrid
- Private Sub fillGrid() 'fill the data grid
- Dim encryption As New clsEncryption 'instantiate the encryption form
- lineCount = File.ReadAllLines("loginData.txt").Length 'make the linecount be the amount of records in the file
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- Dim record As String 'record from the file
- Dim fields() As String 'fields in the record
- dgv_loginData.Rows.Clear() 'empty the data grid
- Dim sr As New StreamReader("loginData.txt") 'stream reader of the login data file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'decrypt record
- fields = record.Split(",") 'split record
- Dim index = dgv_loginData.Rows.Add 'the row to add to
- dgv_loginData.Rows(index).SetValues(fields) 'add a record to the datagrid by putting each field in its correct place
- End While
- sr.Close() 'close stream reader
- dgv_loginData.RowHeadersVisible = False 'Removes the symbols by the rows
- dgv_loginData.ClearSelection() 'Removes the blue selection box
- dgv_loginData.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
- rowIndex = lineCount - 1 'show the currenty corrected row
- txt_username.Text = "Username" 'reset text boxes
- txt_password.Text = "Password"
- txt_accountID.Text = "AccountID"
- txt_levelOfAccess.Text = "Level of Access"
- End Sub
- Private Sub LoginData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
- Me.CenterToScreen() 'center form to screen
- Me.BackColor = Color.FromArgb(247, 220, 143) 'Doesnt need other options as only administrators can access this form
- Dim startupPath As String = Application.StartupPath 'stores location the program is running
- pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'set image locations
- pbx_back.ImageLocation = startupPath & "\Arrow.png"
- If Dir(startupPath & "\tempLoginData.txt") = "" Then 'if temp file doesnt exist
- Dim sw As New StreamWriter("tempLoginData.txt", False) 'make file with stream writer
- sw.Close() 'close stream writer
- End If
- End Sub
- Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
- If txt_search.Text = "Search by Username" Then 'if text is start text
- txt_search.Text = "" 'clear text box
- End If
- End Sub
- Private Sub txt_username_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_username.MouseClick
- If txt_username.Text = "Username" Then 'same as txt_search
- txt_username.Text = ""
- End If
- End Sub
- Private Sub txt_password_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_password.MouseClick
- If txt_password.Text = "Password" Then
- txt_password.Text = ""
- End If
- End Sub
- Private Sub txt_accountID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_accountID.MouseClick
- If txt_accountID.Text = "AccountID" Then
- txt_accountID.Text = ""
- End If
- End Sub
- Private Sub txt_levelOfAccess_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_levelOfAccess.MouseClick
- If txt_levelOfAccess.Text = "Level of Access" Then
- txt_levelOfAccess.Text = ""
- End If
- End Sub
- Private Sub pbx_back_Click(sender As Object, e As EventArgs) Handles pbx_back.Click
- Dim MainMenu As New MainMenu 'new instance of the main menu form
- MainMenu.Show() 'show the new form
- Me.Close() 'close the current form
- End Sub
- Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'rollover
- End Sub
- Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\Arrow.png" 'normal image
- End Sub
- Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
- fillGrid() 'fill the data grid
- End Sub
- Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
- Dim encryption As New clsEncryption 'instantiate classes
- Dim validation As New clsValidation
- Dim validated As Boolean = True 'is validation met
- Dim record As String 'record from file
- Dim fields() As String 'fields from record
- Dim addAccountID As Integer = 0 'ID to be added
- Dim addUsername As String 'account data to be added
- Dim addPassword As String
- Dim addLevelOfAccess As String
- Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'decrypt record
- fields = record.Split(",") 'split record into fields
- If fields(0) >= addAccountID Then 'if the ID is bigger than the ID being added
- addAccountID = fields(0) + 1 'Makes sure the AccountID being added is always 1 greater than the current largest stored
- End If
- End While
- sr.Close() 'close stream reader
- If validation.characterCheck(txt_username.Text, ",") = False Or validation.characterCheck(txt_password.Text, ",") = False Or validation.characterCheck(txt_levelOfAccess.Text, ",") = False Then 'doesnt allow for commas to be entered
- validated = False 'validation to not contain commas
- MsgBox("Data cannot contain commas") 'notify user
- End If
- If validation.presenceCheck(txt_username.Text) = True And txt_username.Text <> "Username" Then 'presence check
- addUsername = txt_username.Text 'set value
- Else
- validated = False 'validation isnt met
- End If
- If validation.presenceCheck(txt_password.Text) = True And txt_password.Text <> "Password" Then
- addPassword = txt_password.Text 'same as username
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_levelOfAccess.Text) = True And txt_levelOfAccess.Text <> "Level of Access" Then
- addLevelOfAccess = txt_levelOfAccess.Text
- Else
- validated = False
- End If
- If validated = True Then 'if data is validated
- Dim sw As New StreamWriter("loginData.txt", True) 'stream writer to append login data file
- sw.WriteLine(encryption.encrypt(addAccountID & "," & addUsername & "," & addPassword & "," & addLevelOfAccess)) 'add a record to the file
- sw.Close() 'close stream writer
- Else
- MsgBox("You need to fill out all fields") 'notify user
- End If
- fillGrid() 'fill the data grid
- End Sub
- Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
- Dim encryption As New clsEncryption 'instantiate classes
- Dim validation As New clsValidation
- lineCount = File.ReadAllLines("loginData.txt").Length 'the amount of records in the file
- Dim found As Boolean = False 'if found
- Dim record As String 'record from file
- Dim fields() As String 'fields in record
- Dim recordCount As Integer = 0 'amount of records
- Dim sr As New StreamReader("loginData.txt") 'stream reader of the login data file
- If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by Username" Then 'presence check
- While sr.Peek() >= 0 'while there are records to read
- recordCount = recordCount + 1 'count the amount of records stored
- record = encryption.decrypt(sr.ReadLine) 'decrypt record
- fields = record.Split(",") 'split record
- If fields(1) = txt_search.Text Then 'if username is found
- dgv_loginData.ClearSelection() 'unselect
- dgv_loginData.Rows(recordCount - 1).Selected = True 'make the found record be selected
- found = True 'has been found
- ElseIf recordCount = lineCount And found = False Then 'if not found
- MsgBox("There is no account stored with that username") 'notify user
- End If
- End While
- Else
- MsgBox("You must fill out all fields") 'notify user
- End If
- txt_search.Text = "Search by Username" 'reset text box
- End Sub
- Private Sub btn_overwrite_Click(sender As Object, e As EventArgs) Handles btn_overwrite.Click
- Dim swTemp As New StreamWriter("tempBooks.txt", False) 'empties the tempLoginData file for use in this subroutine
- swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
- Dim validation As New clsValidation 'instantiate classes
- Dim encryption As New clsEncryption
- Dim editUsername As String 'data to overwrite with
- Dim editPassword As String
- Dim editLevelOfAccess As String
- Dim validated As Boolean = True 'is data validated
- Dim IDValidated As Boolean = True 'is ID validated
- Dim searchID As String 'ID being searched for
- Dim record As String 'record from file
- Dim fields() As String 'fields from record
- Dim found As Boolean = False 'is data found
- If validation.presenceCheck(txt_username.Text) = True And txt_username.Text <> "Username" Then 'makes sure that the data to overwrite with makes sense
- editUsername = txt_username.Text 'set value
- Else
- validated = False 'validation isnt met
- MsgBox("Must have a value for the new Username") 'notify user
- End If
- If validation.presenceCheck(txt_password.Text) = True And txt_password.Text <> "Password" Then
- editPassword = txt_password.Text 'same as username
- Else
- validated = False
- MsgBox("Must have a value for the new Password")
- End If
- If validation.presenceCheck(txt_levelOfAccess.Text) = True And txt_levelOfAccess.Text <> "Level of Access" Then
- editLevelOfAccess = txt_levelOfAccess.Text
- Else
- validated = False
- MsgBox("Must have a value for the new level of access")
- End If
- If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then 'Make sure and ID is entered
- searchID = txt_accountID.Text
- Else
- IDValidated = False
- MsgBox("Must enter an ID to replace the record of")
- End If
- If IDValidated = True And validated = True Then 'If all validation is true
- Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
- Dim swTemp2 As New StreamWriter("tempLoginData.txt", True) 'stream writer of temp file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
- fields = record.Split(",") 'split record into fields
- If fields(0) <> searchID Then 'if ID is not in record
- swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
- Else
- swTemp2.WriteLine(encryption.encrypt(searchID & "," & editUsername & "," & editPassword & "," & editLevelOfAccess)) 'adds new edited data to the temp file
- found = True 'has been found
- End If
- End While
- sr.Close() 'close stream readers and writers
- swTemp2.Close()
- Else
- MsgBox("Must meet all validation requirements") 'notify user
- End If
- If found = True Then 'if record was found
- Dim srTemp As New StreamReader("tempLoginData.txt") 'reader of temp file
- Dim sw As New StreamWriter("LoginData.txt", False) 'will overwrite previous file
- sw.Close()
- Dim sw1 As New StreamWriter("loginData.txt", True) 'stream writer to append file
- While srTemp.Peek() >= 0
- record = srTemp.ReadLine 'each record from the temp file
- sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
- End While
- srTemp.Close() 'close stream readers and writers
- sw1.Close()
- Else
- MsgBox("No record found with that ID") 'notify user
- End If
- fillGrid() 'fill data grid
- End Sub
- Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
- dgv_loginData.ClearSelection()
- dgv_loginData.Rows(0).Selected = True 'gp back to the first record
- rowIndex = 0
- End Sub
- Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
- dgv_loginData.ClearSelection()
- If rowIndex = 0 Then
- MsgBox("You are already looking at the first record")
- Else
- rowIndex = rowIndex - 1
- dgv_loginData.Rows(rowIndex).Selected = True 'go one record back unless you are at the fist one already
- End If
- End Sub
- Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
- dgv_loginData.ClearSelection()
- If rowIndex = lineCount - 1 Then
- MsgBox("You are already looking at the last record")
- Else
- rowIndex = rowIndex + 1
- dgv_loginData.Rows(rowIndex).Selected = True 'go on record forwards unless you are at the last one already
- End If
- End Sub
- Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
- dgv_loginData.ClearSelection()
- dgv_loginData.Rows(lineCount - 1).Selected = True 'go to the last record stored
- rowIndex = lineCount - 1
- End Sub
- Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
- Dim swTemp As New StreamWriter("tempBooks.txt", False) 'empties the tempLoginData file for use in this subroutine
- swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
- Dim validation As New clsValidation 'instantiage classes
- Dim encryption As New clsEncryption
- Dim found As Boolean = False 'has data been found
- Dim validated As Boolean = True 'validation
- Dim IDValidated As Boolean = True
- Dim searchID As String 'ID to search for
- Dim record As String 'record from file
- Dim fields() As String 'fields from record
- If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then 'Make sure and ID is entered
- searchID = txt_accountID.Text 'set value
- Else
- IDValidated = False 'validation isnt met
- MsgBox("Must enter an ID to replace the record of") 'notify user
- End If
- If IDValidated = True And validated = True Then 'If all validation is true
- found = True
- Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
- Dim swTemp2 As New StreamWriter("tempLoginData.txt", True) 'stream writer of temp file
- While sr.Peek() >= 0 'while there are recrords to read
- record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
- fields = record.Split(",") 'split into fields
- If fields(0) <> searchID Then 'if ID isnt in record
- swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file but ignores line that is to be deleted
- End If
- End While
- sr.Close() 'close stream readers and writers
- swTemp2.Close()
- Else
- MsgBox("Must meet all validation requirements") 'notify user
- End If
- If found = True Then
- Dim srTemp As New StreamReader("tempLoginData.txt") 'stream reader of temp file
- Dim sw As New StreamWriter("LoginData.txt", False) 'will overwrite previous file
- sw.Close() 'close stream writer
- Dim sw1 As New StreamWriter("loginData.txt", True) 'stream writer to append file
- While srTemp.Peek() >= 0
- record = srTemp.ReadLine 'each record from the temp file
- sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
- End While
- srTemp.Close() 'close stream readers and writers
- sw1.Close()
- End If
- fillGrid() 'fill data grid
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement