ElliottE4

Untitled

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