Advertisement
ElliottE4

Untitled

Apr 7th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 21.58 KB | None | 0 0
  1. Imports System.IO 'imports the system io library in order to use the stream reader/writer commands
  2.  
  3. Public Class Staff
  4.  
  5.     Dim StaffData As String 'creates a new string variable for the textfile data to be stored in
  6.     Dim LineCount As Integer 'creates a new integer variabe that will store the number of lines in the textfile
  7.     Dim RowIndex As Integer = 0 'creates a new integer variable that will store the index of the current row, and it is set to 0 to begin
  8.  
  9.     Private Sub DataGridRefresh() 'creates a new sub that can be called when the data grid needs to be refreshed
  10.  
  11.         StaffData = Application.StartupPath & "\staff.txt" 'sets the staffData variable equal to the file path of the staff text file
  12.         If Dir(StaffData) = "" Then 'checks if the staff text file directory has no data
  13.  
  14.             'Opening a streamwriter creates a new textfile if there isn't a textfile to be written in
  15.             Dim sw As New StreamWriter(StaffData, True) 'false will overwrite text, so use true unless you need to remove text
  16.             sw.Close() 'closes the streamwriter to avoid data loss
  17.             MsgBox("File has been created") 'shows message to the user to make them aware that a new file has been created
  18.  
  19.         End If
  20.  
  21.         Dim NewRecord As String 'creates a new string variable that will store the data in a record in the texfile
  22.         Dim Fields() As String 'creates a new string array to store each record with comma splits to identify each field
  23.  
  24.         dgv_Staff.Rows.Clear() 'clears the data grid
  25.  
  26.         LineCount = File.ReadAllLines(StaffData).Length 'makes the linecount equal to the number of lines in the textfile
  27.  
  28.         Dim sr As New StreamReader(StaffData) 'opens a new streamwreader to read the data in the textfile
  29.  
  30.         While sr.Peek >= 0 'creates a loop for each line until the end of the file
  31.  
  32.             NewRecord = sr.ReadLine 'the newrecord variable stores the data on the currently viewed line (record) in the text file
  33.             Fields = NewRecord.Split(",") 'the fields array stores each field in the record, by splitting the data at each comma
  34.  
  35.             Dim index = dgv_Staff.Rows.Add 'creates a new variable that adds data to the data grid
  36.  
  37.             dgv_Staff.Rows(index).SetValues(Decryption(Fields(0)), Decryption(Fields(1)), Decryption(Fields(2)), Decryption(Fields(3)), Decryption(Fields(4)), Decryption(Fields(5)), Decryption(Fields(6)), Decryption(Fields(7)))
  38.             'adds values to the datagrid for each field in the record, and also decrypts them so that they can be viewed
  39.  
  40.         End While
  41.         sr.Close() 'closes streamreader to avoid data loss
  42.  
  43.         dgv_Staff.RowHeadersVisible = False 'turns off the row headers
  44.         dgv_Staff.ClearSelection() 'unselects any highlighted rows in the datagrid
  45.  
  46.     End Sub
  47.  
  48.     Private Sub Staff_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  49.  
  50.         If Security = 1 Then
  51.  
  52.             btn_Add.Visible = False
  53.             btn_Edit.Visible = False
  54.             btn_Delete.Visible = False
  55.             btn_save.Visible = False
  56.             btn_Search.Visible = False
  57.             btn_Sort.Visible = False
  58.             dgv_Staff.Visible = False
  59.  
  60.         End If
  61.  
  62.         'when the staff form loads, the data grid will be refreshed by calling the subroutine
  63.         DataGridRefresh()
  64.  
  65.     End Sub
  66.  
  67.     Private Sub pbx_ExitButton_Click(sender As Object, e As EventArgs) Handles pbx_ExitButton.Click
  68.  
  69.         If MsgBox("Are you sure you want to exit to the menu form?", vbYesNo) = vbYes Then 'shows the user a message box that asks if they want to exit to the menu form
  70.  
  71.             'when the exit button is clicked, exit the form if yes is selected
  72.             Dim menu As New LibraryMenu 'creates new instance of the menu form
  73.             menu.Show() 'shows the menu form
  74.             Me.Close() 'closes the currrent form
  75.  
  76.  
  77.         End If
  78.  
  79.     End Sub
  80.  
  81.     Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
  82.  
  83.         Dim UniqueUsername As Boolean = True 'creates a new boolean variable that is used to chek if the username added is unique
  84.         Dim validation As New Validation
  85.  
  86.         For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
  87.  
  88.             If txt_Username.Text = dgv_Staff.Rows(i).Cells(1).Value Then 'if the data in the username textbox matches the second field of the currently selected record, then:
  89.  
  90.                 UniqueUsername = False
  91.  
  92.             End If
  93.  
  94.         Next
  95.  
  96.         'if every textbox has no data inputed / presence check
  97.         If txt_StaffID.Text = "" Or txt_Username.Text = "" Or txt_Password.Text = "" Or txt_Address.Text = "" Or txt_Tel.Text = "" Or txt_AccessLevel.Text = "" Or txt_SecurityQuestion.Text = "" Or txt_SecurityAnswer.Text = "" Then
  98.  
  99.  
  100.             MsgBox("Please input data into all textboxes") 'shows message to ask user to input data into texboxes
  101.             txt_StaffID.Clear() 'clears textboxes
  102.             txt_Username.Clear()
  103.             txt_Password.Clear()
  104.             txt_Address.Clear()
  105.             txt_Tel.Clear()
  106.             txt_AccessLevel.Clear()
  107.             txt_SecurityQuestion.Clear()
  108.             txt_SecurityAnswer.Clear()
  109.  
  110.         ElseIf validation.lengthCheck(txt_Tel.Text, 11, "Telephone number", "Limit") = False Then
  111.  
  112.             txt_Tel.Clear()
  113.  
  114.         ElseIf UniqueUsername = False Then
  115.  
  116.             MsgBox("Username already exists. Please use another username") 'prompts the user to use a unique username
  117.             txt_Username.Clear()
  118.  
  119.         Else 'if there is data
  120.  
  121.             Dim sw As New StreamWriter(StaffData, True) 'opens new streamwriter
  122.  
  123.             'adds the information in the textboxes to the textfile using streamwrite, with commas to split it up
  124.             sw.WriteLine(Encryption(txt_StaffID.Text) & "," & Encryption(txt_Username.Text) & "," & Encryption(txt_Password.Text) & "," & Encryption(txt_Address.Text) & "," & Encryption(txt_Tel.Text) & "," & Encryption(txt_AccessLevel.Text) & "," & Encryption(txt_SecurityQuestion.Text) & "," & Encryption(txt_SecurityAnswer.Text))
  125.             sw.Close()
  126.  
  127.             MsgBox("New file added") 'shows the user that the file has been added to database
  128.  
  129.             'clear all data in the textboxes
  130.             txt_StaffID.Text = ""
  131.             txt_Username.Text = ""
  132.             txt_Password.Text = ""
  133.             txt_Address.Text = ""
  134.             txt_Tel.Text = ""
  135.             txt_AccessLevel.Text = ""
  136.             txt_SecurityQuestion.Text = ""
  137.             txt_SecurityAnswer.Text = ""
  138.  
  139.  
  140.             DataGridRefresh() 'refreshes data grid
  141.  
  142.         End If
  143.  
  144.     End Sub
  145.  
  146.     Private Sub btn_Search_Click(sender As Object, e As EventArgs) Handles btn_Search.Click
  147.  
  148.         Dim i As Integer 'creates new integer variable to be used in a loop
  149.  
  150.  
  151.         If txt_StaffID.Text = "" Then 'if there is no data inputted into the staffid textbox
  152.  
  153.             MsgBox("Please input a staffID to search in the textbox and try again.") 'prompts the user to enter data
  154.  
  155.         ElseIf txt_StaffID.Text > LineCount - 1 Or txt_StaffID.Text < 0 Then
  156.  
  157.             MsgBox("Please input a valid staffID to search in the textbox and try again.") 'prompts the user to enter data
  158.  
  159.         Else 'when there is data in the staffid textbox
  160.  
  161.             For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
  162.  
  163.                 If txt_StaffID.Text = dgv_Staff.Rows(i).Cells(0).Value Then 'if the data in the staffid textbox matches the first field of the currently selected record, then:
  164.  
  165.                     dgv_Staff.Rows(i).Selected = True 'highlight the currrently selected row
  166.  
  167.                     'make each textbox show the corresponding field from the data grid:
  168.                     txt_StaffID.Text = dgv_Staff.Rows(i).Cells(0).Value
  169.                     txt_Username.Text = dgv_Staff.Rows(i).Cells(1).Value
  170.                     txt_Password.Text = dgv_Staff.Rows(i).Cells(2).Value
  171.                     txt_Address.Text = dgv_Staff.Rows(i).Cells(3).Value
  172.                     txt_Tel.Text = dgv_Staff.Rows(i).Cells(4).Value
  173.                     txt_AccessLevel.Text = dgv_Staff.Rows(i).Cells(5).Value
  174.                     txt_SecurityQuestion.Text = dgv_Staff.Rows(i).Cells(6).Value
  175.                     txt_SecurityAnswer.Text = dgv_Staff.Rows(i).Cells(7).Value
  176.  
  177.  
  178.                 End If
  179.  
  180.             Next
  181.             DataGridRefresh() 'refreshes data grid
  182.  
  183.             dgv_Staff.Rows(txt_StaffID.Text - 1).Selected = True 'highlights the row of the data grid view that matches the staffID
  184.             'staffid.text - 1 because the data grid starts with 0
  185.  
  186.  
  187.  
  188.         End If
  189.  
  190.     End Sub
  191.  
  192.     Private Sub btn_Edit_Click(sender As Object, e As EventArgs) Handles btn_Edit.Click
  193.  
  194.         'shows the selected record in the textboxes to be edited
  195.  
  196.         txt_StaffID.Text = dgv_Staff.Rows(RowIndex).Cells(0).Value 'each textbox shows the data from the corresponding field in the record currently selected
  197.         txt_Username.Text = dgv_Staff.Rows(RowIndex).Cells(1).Value
  198.         txt_Password.Text = dgv_Staff.Rows(RowIndex).Cells(2).Value
  199.         txt_Address.Text = dgv_Staff.Rows(RowIndex).Cells(3).Value
  200.         txt_Tel.Text = dgv_Staff.Rows(RowIndex).Cells(4).Value
  201.         txt_AccessLevel.Text = dgv_Staff.Rows(RowIndex).Cells(5).Value
  202.         txt_SecurityQuestion.Text = dgv_Staff.Rows(RowIndex).Cells(6).Value
  203.         txt_SecurityAnswer.Text = dgv_Staff.Rows(RowIndex).Cells(7).Value
  204.  
  205.         MsgBox("Edit the data in the textboxes, then click the save edited data button when finished") 'shows a message box to tell the user what to do
  206.  
  207.     End Sub
  208.  
  209.     Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
  210.  
  211.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  212.         Dim i As Integer 'creates new integer variable to be used in a loop
  213.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  214.  
  215.         fileLines = File.ReadAllLines(StaffData)  'saves all of the data in the textfile to the variable
  216.  
  217.         For i = 0 To fileLines.Length - 1 'starts loop to read all of the lines
  218.  
  219.             fieldSplit = fileLines(i).Split(",") 'splits each record into each field using the commas
  220.  
  221.             If fieldSplit.Length > 0 AndAlso Decryption(fieldSplit(0)) = txt_StaffID.Text Then 'when there is data in the textfile, and the staffID matches:
  222.  
  223.                 'changes each field to match the new data in the textboxes
  224.                 'The data is also encrypted as the data in the datagrid view is decrypted, so it must be encrypted to be written
  225.                 fieldSplit(0) = Encryption(txt_StaffID.Text)
  226.                 fieldSplit(1) = Encryption(txt_Username.Text)
  227.                 fieldSplit(2) = Encryption(txt_Password.Text)
  228.                 fieldSplit(3) = Encryption(txt_Address.Text)
  229.                 fieldSplit(4) = Encryption(txt_Tel.Text)
  230.                 fieldSplit(5) = Encryption(txt_AccessLevel.Text)
  231.                 fieldSplit(6) = Encryption(txt_SecurityQuestion.Text)
  232.                 fieldSplit(7) = Encryption(txt_SecurityAnswer.Text)
  233.  
  234.                 fileLines(i) = String.Join(",", fieldSplit) 'joins all data in each line back together with commas to split it
  235.  
  236.             End If
  237.         Next
  238.  
  239.         File.WriteAllLines(StaffData, fileLines) 'writes all the data back to the textfile
  240.         DataGridRefresh() 'refreshes datgrid
  241.  
  242.     End Sub
  243.  
  244.     Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
  245.  
  246.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  247.         Dim i As Integer 'creates new integer variable to be used in a loop
  248.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  249.  
  250.  
  251.         fileLines = File.ReadAllLines(StaffData) 'saves all of the data in the textfile to the variable
  252.  
  253.         If MsgBox("Are you sure you want to delete?", vbYesNo) = vbYes Then 'shows message box to ask the user if they are sure they want to delete the record
  254.  
  255.             'if yes was clicked in the messsage box:
  256.  
  257.             If txt_StaffID.Text = "" Then 'if the staffid textbox is empty then:
  258.  
  259.                 MsgBox("Please input a staffID to delete in the textbox and try again.") 'shows the user a message box prompt to tell them to input data
  260.  
  261.             Else
  262.  
  263.                 Dim sw As New StreamWriter(StaffData, False) 'opens new streamwriter using false instead of true in order to delete data
  264.                 For i = 0 To fileLines.Length - 1 'starts new loop for every line in the textfile
  265.  
  266.                     fieldSplit = fileLines(i).Split(",") 'makes the fieldsplit array store each field in the current record by splitting the record at each comma
  267.  
  268.                     If txt_StaffID.Text <> dgv_Staff.Rows(i).Cells(0).Value AndAlso fieldSplit(i).Length > 0 Then
  269.  
  270.                         'if the data in the staffID textbox doesn't match the currently seleted record, and if the length of the field is more than 0, then:
  271.                         'the data will be written back to the textfile
  272.                         sw.WriteLine(Encryption(dgv_Staff.Rows(i).Cells(0).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(1).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(2).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(3).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(4).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(5).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(6).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(7).Value))
  273.  
  274.                     End If
  275.  
  276.                     'if the staffid in the textbox does match the currently selected record, it will be skipped from being re-written to the textfile
  277.  
  278.                 Next
  279.                 sw.Close() 'closes the streamwriter to avoid data loss
  280.                 DataGridRefresh() 'refreshes the data grid
  281.  
  282.             End If
  283.  
  284.         Else 'if the user choose "no" to continuing to delete the file, then:
  285.  
  286.             MsgBox("Data will not be deleted") 'shows a message box to confirm that the data will not be deleted
  287.  
  288.         End If
  289.  
  290.     End Sub
  291.  
  292.     Private Sub btn_up_Click(sender As Object, e As EventArgs) Handles btn_up.Click
  293.  
  294.         If RowIndex > 0 Then 'if the current row selected is more than the first row
  295.  
  296.             RowIndex = RowIndex - 1 'decrease the row index by 1 which moves to the previous row
  297.  
  298.         Else
  299.  
  300.             MsgBox("The first record is selected") 'gives the user a message that tells them that the first row is already selected
  301.  
  302.         End If
  303.  
  304.         dgv_Staff.ClearSelection() 'this clears which row is highlighted
  305.         dgv_Staff.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  306.  
  307.     End Sub
  308.  
  309.     Private Sub btn_Down_Click(sender As Object, e As EventArgs) Handles btn_Down.Click
  310.  
  311.         If RowIndex < LineCount - 1 Then 'if the current row selected is less than the number of rows then:
  312.  
  313.             RowIndex = RowIndex + 1 'increase index of the row by 1, which moves to the next row
  314.  
  315.         Else
  316.  
  317.             MsgBox("The last record is selected") 'this gives a message to the user that they alrady have the last record selected
  318.  
  319.         End If
  320.  
  321.         dgv_Staff.ClearSelection() 'this clears which row is highlighted
  322.         dgv_Staff.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  323.  
  324.     End Sub
  325.  
  326.     Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
  327.  
  328.         'clears all textboxes
  329.         txt_StaffID.Clear()
  330.         txt_Username.Clear()
  331.         txt_Password.Clear()
  332.         txt_Address.Clear()
  333.         txt_Tel.Clear()
  334.         txt_AccessLevel.Clear()
  335.         txt_SecurityQuestion.Clear()
  336.         txt_SecurityAnswer.Clear()
  337.  
  338.         DataGridRefresh() 'refreshes data grid
  339.  
  340.     End Sub
  341.  
  342.     Private Sub btn_timetable_Click(sender As Object, e As EventArgs) Handles btn_timetable.Click
  343.  
  344.         Dim timetable As New Timetable 'creates new instance of the timetable form
  345.         timetable.Show() 'shows the timetable form
  346.         Me.Close() 'closes the currrent form
  347.  
  348.     End Sub
  349.  
  350.     Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
  351.  
  352.         Dim i As Integer 'creates an integer variable to be used as a counter
  353.         Dim sortingArray(7) As String 'creates an array to store temporary data in order to swap the data in other arrays
  354.         Dim currentRecord(7) As String 'creates an array to store the data in the currently viewed record in the loop
  355.         Dim nextRecord(7) As String 'creates an array to store the data in the next record after the current record in the loop
  356.  
  357.         Dim sw1 As New StreamWriter(StaffData, False) 'deletes the data in the textfile
  358.         sw1.Close() 'closes streamwriter
  359.  
  360.         For i = 0 To LineCount - 1 'starts a loop to view every line in the textfile
  361.  
  362.             For counter = 0 To LineCount - 2 'creates another loop with linecount - 2 so that it gets to the second last row in the textfile (won't need to swap if it gets to the last row)
  363.  
  364.                 'makes the currentrecord array store every field of the current record
  365.                 currentRecord(0) = dgv_Staff.Rows(counter).Cells(0).Value
  366.                 currentRecord(1) = dgv_Staff.Rows(counter).Cells(1).Value
  367.                 currentRecord(2) = dgv_Staff.Rows(counter).Cells(2).Value
  368.                 currentRecord(3) = dgv_Staff.Rows(counter).Cells(3).Value
  369.                 currentRecord(4) = dgv_Staff.Rows(counter).Cells(4).Value
  370.                 currentRecord(5) = dgv_Staff.Rows(counter).Cells(5).Value
  371.                 currentRecord(6) = dgv_Staff.Rows(counter).Cells(6).Value
  372.                 currentRecord(7) = dgv_Staff.Rows(counter).Cells(7).Value
  373.  
  374.                 'makes the nextrecord array store every field in the next record
  375.                 nextRecord(0) = dgv_Staff.Rows(counter + 1).Cells(0).Value
  376.                 nextRecord(1) = dgv_Staff.Rows(counter + 1).Cells(1).Value
  377.                 nextRecord(2) = dgv_Staff.Rows(counter + 1).Cells(2).Value
  378.                 nextRecord(3) = dgv_Staff.Rows(counter + 1).Cells(3).Value
  379.                 nextRecord(4) = dgv_Staff.Rows(counter + 1).Cells(4).Value
  380.                 nextRecord(5) = dgv_Staff.Rows(counter + 1).Cells(5).Value
  381.                 nextRecord(6) = dgv_Staff.Rows(counter + 1).Cells(6).Value
  382.                 nextRecord(7) = dgv_Staff.Rows(counter + 1).Cells(7).Value
  383.  
  384.                 If currentRecord(0) > nextRecord(0) Then 'if the first field in the current record is larger than the first field in the next array:
  385.  
  386.                     sortingArray = currentRecord 'makes the temporary sorting array store the data in the current record
  387.                     currentRecord = nextRecord 'the current record will then store the data from the next record
  388.                     nextRecord = sortingArray 'the next record will store the data from the current record
  389.  
  390.                     'this is essentially a swap of the data in the two arrays
  391.  
  392.                 End If
  393.  
  394.                 dgv_Staff.Rows(counter).SetValues(currentRecord) 'sets the currently viewed row in the datagrid to the data in the currentrecord array
  395.                 dgv_Staff.Rows(counter + 1).SetValues(nextRecord) 'sets the next row in the datagrid to the data in the nextrecord array
  396.  
  397.  
  398.  
  399.             Next
  400.  
  401.  
  402.             Dim sw As New StreamWriter(StaffData, True) 'opens new streamwriter to write data to the textfi;e
  403.  
  404.             'adds each sorted line in the datagrid to the textfile using the loop
  405.             sw.WriteLine(Encryption(dgv_Staff.Rows(i).Cells(0).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(1).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(2).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(3).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(4).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(5).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(6).Value) & "," & Encryption(dgv_Staff.Rows(i).Cells(7).Value))
  406.             sw.Close() 'closes streamwriter to avoid data loss
  407.  
  408.         Next
  409.  
  410.     End Sub
  411.  
  412.     Private Sub btn_Help_Click(sender As Object, e As EventArgs) Handles btn_Help.Click
  413.  
  414.         Help.txt_help.Text = "This is the staff form. On the right of the form is a data grid viewm, which displays the staff database. " _
  415.             & "You can navigate this by using either the arrow buttons underneath, or by searching for a specific staffID. " _
  416.             & "To search, you need to input a staffID into the staffID textbox, then click the search button. To add data to the database, " _
  417.             & "You will need to input all of the correct data into the corresponding textboxes on the left side of the form, then click the add button. " _
  418.             & "If you wish to delete data from the database, you can input the staffID of the record you wish to delete into the staffID textbox, then click the delete button. " _
  419.             & "If you wish to edit data that is already in the database, navigate to the record you wish to edit, then click the edit button. " _
  420.             & "The existing data in that record will be displayed in the textboxes, and you can edit the data, then click the 'save edited data' button to save your edit. " _
  421.             & "If you want to clear the textboxes of unnecessary data, you can click the clear textboxes button to do this. " _
  422.             & "If the data is in an incorrect order (it should be chronological by staffID), click the sort button to sort the data. " _
  423.             & "To view the staff timetable, click the timetable button. " _
  424.             & "Finally, to exit back to the menu form, you can click the cross button in the top right of the form."
  425.  
  426.         Help.Show()
  427.  
  428.     End Sub
  429. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement