Advertisement
ElliottE4

Untitled

Feb 18th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 21.38 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.         Else 'when there is data in the staffid textbox
  156.  
  157.             For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
  158.  
  159.                 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:
  160.  
  161.                     dgv_Staff.Rows(i).Selected = True 'highlight the currrently selected row
  162.  
  163.                     'make each textbox show the corresponding field from the data grid:
  164.                     txt_StaffID.Text = dgv_Staff.Rows(i).Cells(0).Value
  165.                     txt_Username.Text = dgv_Staff.Rows(i).Cells(1).Value
  166.                     txt_Password.Text = dgv_Staff.Rows(i).Cells(2).Value
  167.                     txt_Address.Text = dgv_Staff.Rows(i).Cells(3).Value
  168.                     txt_Tel.Text = dgv_Staff.Rows(i).Cells(4).Value
  169.                     txt_AccessLevel.Text = dgv_Staff.Rows(i).Cells(5).Value
  170.                     txt_SecurityQuestion.Text = dgv_Staff.Rows(i).Cells(6).Value
  171.                     txt_SecurityAnswer.Text = dgv_Staff.Rows(i).Cells(7).Value
  172.  
  173.  
  174.                 End If
  175.  
  176.             Next
  177.             DataGridRefresh() 'refreshes data grid
  178.  
  179.             dgv_Staff.Rows(txt_StaffID.Text - 1).Selected = True 'highlights the row of the data grid view that matches the staffID
  180.             'staffid.text - 1 because the data grid starts with 0
  181.  
  182.  
  183.  
  184.         End If
  185.  
  186.     End Sub
  187.  
  188.     Private Sub btn_Edit_Click(sender As Object, e As EventArgs) Handles btn_Edit.Click
  189.  
  190.         'shows the selected record in the textboxes to be edited
  191.  
  192.         txt_StaffID.Text = dgv_Staff.Rows(RowIndex).Cells(0).Value 'each textbox shows the data from the corresponding field in the record currently selected
  193.         txt_Username.Text = dgv_Staff.Rows(RowIndex).Cells(1).Value
  194.         txt_Password.Text = dgv_Staff.Rows(RowIndex).Cells(2).Value
  195.         txt_Address.Text = dgv_Staff.Rows(RowIndex).Cells(3).Value
  196.         txt_Tel.Text = dgv_Staff.Rows(RowIndex).Cells(4).Value
  197.         txt_AccessLevel.Text = dgv_Staff.Rows(RowIndex).Cells(5).Value
  198.         txt_SecurityQuestion.Text = dgv_Staff.Rows(RowIndex).Cells(6).Value
  199.         txt_SecurityAnswer.Text = dgv_Staff.Rows(RowIndex).Cells(7).Value
  200.  
  201.         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
  202.  
  203.     End Sub
  204.  
  205.     Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
  206.  
  207.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  208.         Dim i As Integer 'creates new integer variable to be used in a loop
  209.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  210.  
  211.         fileLines = File.ReadAllLines(StaffData)  'saves all of the data in the textfile to the variable
  212.  
  213.         For i = 0 To fileLines.Length - 1 'starts loop to read all of the lines
  214.  
  215.             fieldSplit = fileLines(i).Split(",") 'splits each record into each field using the commas
  216.  
  217.             If fieldSplit.Length > 0 AndAlso Decryption(fieldSplit(0)) = txt_StaffID.Text Then 'when there is data in the textfile, and the staffID matches:
  218.  
  219.                 'changes each field to match the new data in the textboxes
  220.                 'The data is also encrypted as the data in the datagrid view is decrypted, so it must be encrypted to be written
  221.                 fieldSplit(0) = Encryption(txt_StaffID.Text)
  222.                 fieldSplit(1) = Encryption(txt_Username.Text)
  223.                 fieldSplit(2) = Encryption(txt_Password.Text)
  224.                 fieldSplit(3) = Encryption(txt_Address.Text)
  225.                 fieldSplit(4) = Encryption(txt_Tel.Text)
  226.                 fieldSplit(5) = Encryption(txt_AccessLevel.Text)
  227.                 fieldSplit(6) = Encryption(txt_SecurityQuestion.Text)
  228.                 fieldSplit(7) = Encryption(txt_SecurityAnswer.Text)
  229.  
  230.                 fileLines(i) = String.Join(",", fieldSplit) 'joins all data in each line back together with commas to split it
  231.  
  232.             End If
  233.         Next
  234.  
  235.         File.WriteAllLines(StaffData, fileLines) 'writes all the data back to the textfile
  236.         DataGridRefresh() 'refreshes datgrid
  237.  
  238.     End Sub
  239.  
  240.     Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
  241.  
  242.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  243.         Dim i As Integer 'creates new integer variable to be used in a loop
  244.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  245.  
  246.  
  247.         fileLines = File.ReadAllLines(StaffData) 'saves all of the data in the textfile to the variable
  248.  
  249.         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
  250.  
  251.             'if yes was clicked in the messsage box:
  252.  
  253.             If txt_StaffID.Text = "" Then 'if the staffid textbox is empty then:
  254.  
  255.                 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
  256.  
  257.             Else
  258.  
  259.                 Dim sw As New StreamWriter(StaffData, False) 'opens new streamwriter using false instead of true in order to delete data
  260.                 For i = 0 To fileLines.Length - 1 'starts new loop for every line in the textfile
  261.  
  262.                     fieldSplit = fileLines(i).Split(",") 'makes the fieldsplit array store each field in the current record by splitting the record at each comma
  263.  
  264.                     If txt_StaffID.Text <> dgv_Staff.Rows(i).Cells(0).Value AndAlso fieldSplit(i).Length > 0 Then
  265.  
  266.                         '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:
  267.                         'the data will be written back to the textfile
  268.                         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))
  269.  
  270.                     End If
  271.  
  272.                     'if the staffid in the textbox does match the currently selected record, it will be skipped from being re-written to the textfile
  273.  
  274.                 Next
  275.                 sw.Close() 'closes the streamwriter to avoid data loss
  276.                 DataGridRefresh() 'refreshes the data grid
  277.  
  278.             End If
  279.  
  280.         Else 'if the user choose "no" to continuing to delete the file, then:
  281.  
  282.             MsgBox("Data will not be deleted") 'shows a message box to confirm that the data will not be deleted
  283.  
  284.         End If
  285.  
  286.     End Sub
  287.  
  288.     Private Sub btn_up_Click(sender As Object, e As EventArgs) Handles btn_up.Click
  289.  
  290.         If RowIndex > 0 Then 'if the current row selected is more than the first row
  291.  
  292.             RowIndex = RowIndex - 1 'decrease the row index by 1 which moves to the previous row
  293.  
  294.         Else
  295.  
  296.             MsgBox("The first record is selected") 'gives the user a message that tells them that the first row is already selected
  297.  
  298.         End If
  299.  
  300.         dgv_Staff.ClearSelection() 'this clears which row is highlighted
  301.         dgv_Staff.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  302.  
  303.     End Sub
  304.  
  305.     Private Sub btn_Down_Click(sender As Object, e As EventArgs) Handles btn_Down.Click
  306.  
  307.         If RowIndex < LineCount - 1 Then 'if the current row selected is less than the number of rows then:
  308.  
  309.             RowIndex = RowIndex + 1 'increase index of the row by 1, which moves to the next row
  310.  
  311.         Else
  312.  
  313.             MsgBox("The last record is selected") 'this gives a message to the user that they alrady have the last record selected
  314.  
  315.         End If
  316.  
  317.         dgv_Staff.ClearSelection() 'this clears which row is highlighted
  318.         dgv_Staff.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  319.  
  320.     End Sub
  321.  
  322.     Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
  323.  
  324.         'clears all textboxes
  325.         txt_StaffID.Clear()
  326.         txt_Username.Clear()
  327.         txt_Password.Clear()
  328.         txt_Address.Clear()
  329.         txt_Tel.Clear()
  330.         txt_AccessLevel.Clear()
  331.         txt_SecurityQuestion.Clear()
  332.         txt_SecurityAnswer.Clear()
  333.  
  334.         DataGridRefresh() 'refreshes data grid
  335.  
  336.     End Sub
  337.  
  338.     Private Sub btn_timetable_Click(sender As Object, e As EventArgs) Handles btn_timetable.Click
  339.  
  340.         Dim timetable As New Timetable 'creates new instance of the timetable form
  341.         timetable.Show() 'shows the timetable form
  342.         Me.Close() 'closes the currrent form
  343.  
  344.     End Sub
  345.  
  346.     Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
  347.  
  348.         Dim i As Integer 'creates an integer variable to be used as a counter
  349.         Dim sortingArray(7) As String 'creates an array to store temporary data in order to swap the data in other arrays
  350.         Dim currentRecord(7) As String 'creates an array to store the data in the currently viewed record in the loop
  351.         Dim nextRecord(7) As String 'creates an array to store the data in the next record after the current record in the loop
  352.  
  353.         Dim sw1 As New StreamWriter(StaffData, False) 'deletes the data in the textfile
  354.         sw1.Close() 'closes streamwriter
  355.  
  356.         For i = 0 To LineCount - 1 'starts a loop to view every line in the textfile
  357.  
  358.             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)
  359.  
  360.                 'makes the currentrecord array store every field of the current record
  361.                 currentRecord(0) = dgv_Staff.Rows(counter).Cells(0).Value
  362.                 currentRecord(1) = dgv_Staff.Rows(counter).Cells(1).Value
  363.                 currentRecord(2) = dgv_Staff.Rows(counter).Cells(2).Value
  364.                 currentRecord(3) = dgv_Staff.Rows(counter).Cells(3).Value
  365.                 currentRecord(4) = dgv_Staff.Rows(counter).Cells(4).Value
  366.                 currentRecord(5) = dgv_Staff.Rows(counter).Cells(5).Value
  367.                 currentRecord(6) = dgv_Staff.Rows(counter).Cells(6).Value
  368.                 currentRecord(7) = dgv_Staff.Rows(counter).Cells(7).Value
  369.  
  370.                 'makes the nextrecord array store every field in the next record
  371.                 nextRecord(0) = dgv_Staff.Rows(counter + 1).Cells(0).Value
  372.                 nextRecord(1) = dgv_Staff.Rows(counter + 1).Cells(1).Value
  373.                 nextRecord(2) = dgv_Staff.Rows(counter + 1).Cells(2).Value
  374.                 nextRecord(3) = dgv_Staff.Rows(counter + 1).Cells(3).Value
  375.                 nextRecord(4) = dgv_Staff.Rows(counter + 1).Cells(4).Value
  376.                 nextRecord(5) = dgv_Staff.Rows(counter + 1).Cells(5).Value
  377.                 nextRecord(6) = dgv_Staff.Rows(counter + 1).Cells(6).Value
  378.                 nextRecord(7) = dgv_Staff.Rows(counter + 1).Cells(7).Value
  379.  
  380.                 If currentRecord(0) > nextRecord(0) Then 'if the first field in the current record is larger than the first field in the next array:
  381.  
  382.                     sortingArray = currentRecord 'makes the temporary sorting array store the data in the current record
  383.                     currentRecord = nextRecord 'the current record will then store the data from the next record
  384.                     nextRecord = sortingArray 'the next record will store the data from the current record
  385.  
  386.                     'this is essentially a swap of the data in the two arrays
  387.  
  388.                 End If
  389.  
  390.                 dgv_Staff.Rows(counter).SetValues(currentRecord) 'sets the currently viewed row in the datagrid to the data in the currentrecord array
  391.                 dgv_Staff.Rows(counter + 1).SetValues(nextRecord) 'sets the next row in the datagrid to the data in the nextrecord array
  392.  
  393.  
  394.  
  395.             Next
  396.  
  397.  
  398.             Dim sw As New StreamWriter(StaffData, True) 'opens new streamwriter to write data to the textfi;e
  399.  
  400.             'adds each sorted line in the datagrid to the textfile using the loop
  401.             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))
  402.             sw.Close() 'closes streamwriter to avoid data loss
  403.  
  404.         Next
  405.  
  406.     End Sub
  407.  
  408.     Private Sub btn_Help_Click(sender As Object, e As EventArgs) Handles btn_Help.Click
  409.  
  410.         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. " _
  411.             & "You can navigate this by using either the arrow buttons underneath, or by searching for a specific staffID. " _
  412.             & "To search, you need to input a staffID into the staffID textbox, then click the search button. To add data to the database, " _
  413.             & "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. " _
  414.             & "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. " _
  415.             & "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. " _
  416.             & "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. " _
  417.             & "If you want to clear the textboxes of unnecessary data, you can click the clear textboxes button to do this. " _
  418.             & "If the data is in an incorrect order (it should be chronological by staffID), click the sort button to sort the data. " _
  419.             & "To view the staff timetable, click the timetable button. " _
  420.             & "Finally, to exit back to the menu form, you can click the cross button in the top right of the form."
  421.  
  422.         Help.Show()
  423.  
  424.     End Sub
  425. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement