Advertisement
ElliottE4

Untitled

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