Advertisement
UsernameHere1

Untitled

Feb 21st, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 17.30 KB | None | 0 0
  1. Imports System.IO 'imports input and output form
  2.  
  3. Public Class LoginData 'login data form
  4.     Dim lineCount As Integer 'The full amount of records in the file
  5.     Dim rowIndex As Integer 'The currently selected row of the datagrid
  6.     Private Sub fillGrid() 'fill the data grid
  7.         Dim encryption As New clsEncryption 'instantiate the encryption form
  8.         lineCount = File.ReadAllLines("loginData.txt").Length 'make the linecount be the amount of records in the file
  9.         Dim startupPath As String 'stores location the program is running
  10.         startupPath = Application.StartupPath
  11.         Dim record As String 'record from the file
  12.         Dim fields() As String 'fields in the record
  13.         dgv_loginData.Rows.Clear() 'empty the data grid
  14.         Dim sr As New StreamReader("loginData.txt") 'stream reader of the login data file
  15.         While sr.Peek() >= 0 'while there are records to read
  16.             record = encryption.decrypt(sr.ReadLine) 'decrypt record
  17.             fields = record.Split(",") 'split record
  18.             Dim index = dgv_loginData.Rows.Add 'the row to add to
  19.             dgv_loginData.Rows(index).SetValues(fields) 'add a record to the datagrid by putting each field in its correct place
  20.         End While
  21.         sr.Close() 'close stream reader
  22.         dgv_loginData.RowHeadersVisible = False 'Removes the symbols by the rows
  23.         dgv_loginData.ClearSelection() 'Removes the blue selection box
  24.         dgv_loginData.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
  25.         rowIndex = lineCount - 1 'show the currenty corrected row
  26.         txt_username.Text = "Username" 'reset text boxes
  27.         txt_password.Text = "Password"
  28.         txt_accountID.Text = "AccountID"
  29.         txt_levelOfAccess.Text = "Level of Access"
  30.     End Sub
  31.     Private Sub LoginData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
  32.         Me.CenterToScreen() 'center form to screen
  33.         Me.BackColor = Color.FromArgb(247, 220, 143) 'Doesnt need other options as only administrators can access this form
  34.         Dim startupPath As String = Application.StartupPath 'stores location the program is running
  35.         pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'set image locations
  36.         pbx_back.ImageLocation = startupPath & "\Arrow.png"
  37.         If Dir(startupPath & "\tempLoginData.txt") = "" Then 'if temp file doesnt exist
  38.             Dim sw As New StreamWriter("tempLoginData.txt", False) 'make file with stream writer
  39.             sw.Close() 'close stream writer
  40.         End If
  41.     End Sub
  42.     Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
  43.         If txt_search.Text = "Search by Username" Then 'if text is start text
  44.             txt_search.Text = "" 'clear text box
  45.         End If
  46.     End Sub
  47.     Private Sub txt_username_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_username.MouseClick
  48.         If txt_username.Text = "Username" Then 'same as txt_search
  49.             txt_username.Text = ""
  50.         End If
  51.     End Sub
  52.     Private Sub txt_password_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_password.MouseClick
  53.         If txt_password.Text = "Password" Then
  54.             txt_password.Text = ""
  55.         End If
  56.     End Sub
  57.     Private Sub txt_accountID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_accountID.MouseClick
  58.         If txt_accountID.Text = "AccountID" Then
  59.             txt_accountID.Text = ""
  60.         End If
  61.     End Sub
  62.     Private Sub txt_levelOfAccess_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_levelOfAccess.MouseClick
  63.         If txt_levelOfAccess.Text = "Level of Access" Then
  64.             txt_levelOfAccess.Text = ""
  65.         End If
  66.     End Sub
  67.     Private Sub pbx_back_Click(sender As Object, e As EventArgs) Handles pbx_back.Click
  68.         Dim MainMenu As New MainMenu 'new instance of the main menu form
  69.         MainMenu.Show() 'show the new form
  70.         Me.Close()  'close the current form
  71.     End Sub
  72.     Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
  73.         Dim startupPath As String 'stores location the program is running
  74.         startupPath = Application.StartupPath
  75.         pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'rollover
  76.     End Sub
  77.  
  78.     Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
  79.         Dim startupPath As String 'stores location the program is running
  80.         startupPath = Application.StartupPath
  81.         pbx_back.ImageLocation = startupPath & "\Arrow.png" 'normal image
  82.     End Sub
  83.  
  84.     Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
  85.         fillGrid() 'fill the data grid
  86.     End Sub
  87.     Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
  88.         Dim encryption As New clsEncryption 'instantiate classes
  89.         Dim validation As New clsValidation
  90.         Dim validated As Boolean = True 'is validation met
  91.         Dim record As String 'record from file
  92.         Dim fields() As String 'fields from record
  93.         Dim addAccountID As Integer = 0 'ID to be added
  94.         Dim addUsername As String 'account data to be added
  95.         Dim addPassword As String
  96.         Dim addLevelOfAccess As String
  97.         Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
  98.         While sr.Peek() >= 0 'while there are records to read
  99.             record = encryption.decrypt(sr.ReadLine) 'decrypt record
  100.             fields = record.Split(",") 'split record into fields
  101.             If fields(0) >= addAccountID Then 'if the ID is bigger than the ID being added
  102.                 addAccountID = fields(0) + 1 'Makes sure the AccountID being added is always 1 greater than the current largest stored
  103.             End If
  104.         End While
  105.         sr.Close() 'close stream reader
  106.         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
  107.             validated = False 'validation to not contain commas
  108.             MsgBox("Data cannot contain commas") 'notify user
  109.         End If
  110.         If validation.presenceCheck(txt_username.Text) = True And txt_username.Text <> "Username" Then 'presence check
  111.             addUsername = txt_username.Text 'set value
  112.         Else
  113.             validated = False 'validation isnt met
  114.         End If
  115.         If validation.presenceCheck(txt_password.Text) = True And txt_password.Text <> "Password" Then
  116.             addPassword = txt_password.Text 'same as username
  117.         Else
  118.             validated = False
  119.         End If
  120.         If validation.presenceCheck(txt_levelOfAccess.Text) = True And txt_levelOfAccess.Text <> "Level of Access" Then
  121.             addLevelOfAccess = txt_levelOfAccess.Text
  122.         Else
  123.             validated = False
  124.         End If
  125.         If validated = True Then 'if data is validated
  126.             Dim sw As New StreamWriter("loginData.txt", True) 'stream writer to append login data file
  127.             sw.WriteLine(encryption.encrypt(addAccountID & "," & addUsername & "," & addPassword & "," & addLevelOfAccess)) 'add a record to the file
  128.             sw.Close() 'close stream writer
  129.         Else
  130.             MsgBox("You need to fill out all fields") 'notify user
  131.         End If
  132.         fillGrid() 'fill the data grid
  133.     End Sub
  134.     Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
  135.         Dim encryption As New clsEncryption 'instantiate classes
  136.         Dim validation As New clsValidation
  137.         lineCount = File.ReadAllLines("loginData.txt").Length 'the amount of records in the file
  138.         Dim found As Boolean = False 'if found
  139.         Dim record As String 'record from file
  140.         Dim fields() As String 'fields in record
  141.         Dim recordCount As Integer = 0 'amount of records
  142.         Dim sr As New StreamReader("loginData.txt") 'stream reader of the login data file
  143.         If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by Username" Then 'presence check
  144.             While sr.Peek() >= 0 'while there are records to read
  145.                 recordCount = recordCount + 1 'count the amount of records stored
  146.                 record = encryption.decrypt(sr.ReadLine) 'decrypt record
  147.                 fields = record.Split(",") 'split record
  148.                 If fields(1) = txt_search.Text Then 'if username is found
  149.                     dgv_loginData.ClearSelection() 'unselect
  150.                     dgv_loginData.Rows(recordCount - 1).Selected = True 'make the found record be selected
  151.                     found = True 'has been found
  152.                 ElseIf recordCount = lineCount And found = False Then 'if not found
  153.                     MsgBox("There is no account stored with that username") 'notify user
  154.                 End If
  155.             End While
  156.         Else
  157.             MsgBox("You must fill out all fields") 'notify user
  158.         End If
  159.         txt_search.Text = "Search by Username" 'reset text box
  160.     End Sub
  161.     Private Sub btn_overwrite_Click(sender As Object, e As EventArgs) Handles btn_overwrite.Click
  162.         Dim swTemp As New StreamWriter("tempBooks.txt", False) 'empties the tempLoginData file for use in this subroutine
  163.         swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
  164.         Dim validation As New clsValidation 'instantiate classes
  165.         Dim encryption As New clsEncryption
  166.         Dim editUsername As String 'data to overwrite with
  167.         Dim editPassword As String
  168.         Dim editLevelOfAccess As String
  169.         Dim validated As Boolean = True 'is data validated
  170.         Dim IDValidated As Boolean = True 'is ID validated
  171.         Dim searchID As String 'ID being searched for
  172.         Dim record As String 'record from file
  173.         Dim fields() As String 'fields from record
  174.         Dim found As Boolean = False 'is data found
  175.         If validation.presenceCheck(txt_username.Text) = True And txt_username.Text <> "Username" Then 'makes sure that the data to overwrite with makes sense
  176.             editUsername = txt_username.Text 'set value
  177.         Else
  178.             validated = False 'validation isnt met
  179.             MsgBox("Must have a value for the new Username") 'notify user
  180.         End If
  181.         If validation.presenceCheck(txt_password.Text) = True And txt_password.Text <> "Password" Then
  182.             editPassword = txt_password.Text 'same as username
  183.         Else
  184.             validated = False
  185.             MsgBox("Must have a value for the new Password")
  186.         End If
  187.         If validation.presenceCheck(txt_levelOfAccess.Text) = True And txt_levelOfAccess.Text <> "Level of Access" Then
  188.             editLevelOfAccess = txt_levelOfAccess.Text
  189.         Else
  190.             validated = False
  191.             MsgBox("Must have a value for the new level of access")
  192.         End If
  193.         If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then 'Make sure and ID is entered
  194.             searchID = txt_accountID.Text
  195.         Else
  196.             IDValidated = False
  197.             MsgBox("Must enter an ID to replace the record of")
  198.         End If
  199.         If IDValidated = True And validated = True Then 'If all validation is true
  200.             Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
  201.             Dim swTemp2 As New StreamWriter("tempLoginData.txt", True) 'stream writer of temp file
  202.             While sr.Peek() >= 0 'while there are records to read
  203.                 record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
  204.                 fields = record.Split(",") 'split record into fields
  205.                 If fields(0) <> searchID Then 'if ID is not in record
  206.                     swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
  207.                 Else
  208.                     swTemp2.WriteLine(encryption.encrypt(searchID & "," & editUsername & "," & editPassword & "," & editLevelOfAccess)) 'adds new edited data to the temp file
  209.                     found = True 'has been found
  210.                 End If
  211.             End While
  212.             sr.Close() 'close stream readers and writers
  213.             swTemp2.Close()
  214.         Else
  215.             MsgBox("Must meet all validation requirements") 'notify user
  216.         End If
  217.         If found = True Then 'if record was found
  218.             Dim srTemp As New StreamReader("tempLoginData.txt") 'reader of temp file
  219.             Dim sw As New StreamWriter("LoginData.txt", False) 'will overwrite previous file
  220.             sw.Close()
  221.             Dim sw1 As New StreamWriter("loginData.txt", True) 'stream writer to append file
  222.             While srTemp.Peek() >= 0
  223.                 record = srTemp.ReadLine 'each record from the temp file
  224.                 sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
  225.             End While
  226.             srTemp.Close() 'close stream readers and writers
  227.             sw1.Close()
  228.         Else
  229.             MsgBox("No record found with that ID") 'notify user
  230.         End If
  231.         fillGrid() 'fill data grid
  232.     End Sub
  233.     Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
  234.         dgv_loginData.ClearSelection()
  235.         dgv_loginData.Rows(0).Selected = True 'gp back to the first record
  236.         rowIndex = 0
  237.     End Sub
  238.  
  239.     Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
  240.         dgv_loginData.ClearSelection()
  241.         If rowIndex = 0 Then
  242.             MsgBox("You are already looking at the first record")
  243.         Else
  244.             rowIndex = rowIndex - 1
  245.             dgv_loginData.Rows(rowIndex).Selected = True 'go one record back unless you are at the fist one already
  246.         End If
  247.     End Sub
  248.  
  249.     Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
  250.         dgv_loginData.ClearSelection()
  251.         If rowIndex = lineCount - 1 Then
  252.             MsgBox("You are already looking at the last record")
  253.         Else
  254.             rowIndex = rowIndex + 1
  255.             dgv_loginData.Rows(rowIndex).Selected = True 'go on record forwards unless you are at the last one already
  256.         End If
  257.     End Sub
  258.  
  259.     Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
  260.         dgv_loginData.ClearSelection()
  261.         dgv_loginData.Rows(lineCount - 1).Selected = True 'go to the last record stored
  262.         rowIndex = lineCount - 1
  263.     End Sub
  264.  
  265.     Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
  266.         Dim swTemp As New StreamWriter("tempBooks.txt", False) 'empties the tempLoginData file for use in this subroutine
  267.         swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
  268.         Dim validation As New clsValidation 'instantiage classes
  269.         Dim encryption As New clsEncryption
  270.         Dim found As Boolean = False 'has data been found
  271.         Dim validated As Boolean = True 'validation
  272.         Dim IDValidated As Boolean = True
  273.         Dim searchID As String 'ID to search for
  274.         Dim record As String 'record from file
  275.         Dim fields() As String 'fields from record
  276.         If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then 'Make sure and ID is entered
  277.             searchID = txt_accountID.Text 'set value
  278.         Else
  279.             IDValidated = False 'validation isnt met
  280.             MsgBox("Must enter an ID to replace the record of") 'notify user
  281.         End If
  282.         If IDValidated = True And validated = True Then 'If all validation is true
  283.             found = True
  284.             Dim sr As New StreamReader("loginData.txt") 'stream reader of login data file
  285.             Dim swTemp2 As New StreamWriter("tempLoginData.txt", True) 'stream writer of temp file
  286.             While sr.Peek() >= 0 'while there are recrords to read
  287.                 record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
  288.                 fields = record.Split(",") 'split into fields
  289.                 If fields(0) <> searchID Then 'if ID isnt in record
  290.                     swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file but ignores line that is to be deleted
  291.                 End If
  292.             End While
  293.             sr.Close() 'close stream readers and writers
  294.             swTemp2.Close()
  295.         Else
  296.             MsgBox("Must meet all validation requirements") 'notify user
  297.         End If
  298.         If found = True Then
  299.             Dim srTemp As New StreamReader("tempLoginData.txt") 'stream reader of temp file
  300.             Dim sw As New StreamWriter("LoginData.txt", False) 'will overwrite previous file
  301.             sw.Close() 'close stream writer
  302.             Dim sw1 As New StreamWriter("loginData.txt", True) 'stream writer to append file
  303.             While srTemp.Peek() >= 0
  304.                 record = srTemp.ReadLine 'each record from the temp file
  305.                 sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
  306.             End While
  307.             srTemp.Close() 'close stream readers and writers
  308.             sw1.Close()
  309.         End If
  310.         fillGrid() 'fill data grid
  311.     End Sub
  312. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement