Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO 'imports the system io library in order to use the stream reader/writer commands
- Public Class Books
- Dim BookData As String 'creates a new string variable for the textfile data to be stored in
- Dim LineCount As Integer 'creates a new integer variabe that will store the number of lines in the textfile
- 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
- Private Sub DataGridRefresh() 'creates a new sub that can be called when the data grid needs to be refreshed
- BookData = Application.StartupPath & "\books.txt" 'sets the bookData variable equal to the file path of the books text file
- If Dir(BookData) = "" Then 'checks if the book text file directory has no data
- 'Opening a streamwriter creates a new textfile if there isn't a textfile to be written in
- Dim sw As New StreamWriter(BookData, True) 'false will overwrite text, so use true unless you need to remove text
- sw.Close() 'closes the streamwriter to avoid data loss
- MsgBox("File has been created") 'shows message to the user to make them aware that a new file has been created
- End If
- Dim NewRecord As String 'creates a new string variable that will store the data in a record in the texfile
- Dim Fields() As String 'creates a new string array to store each record with comma splits to identify each field
- dgv_Books.Rows.Clear() 'clears the data grid
- LineCount = File.ReadAllLines(BookData).Length 'makes the linecount equal to the number of lines in the textfile
- Dim sr As New StreamReader(BookData) 'opens a new streamwreader to read the data in the textfile
- While sr.Peek >= 0 'creates a loop for each line until the end of the file
- NewRecord = sr.ReadLine 'the newrecord variable stores the data on the currently viewed line (record) in the text file
- Fields = NewRecord.Split(",") 'the fields array stores each field in the record, by splitting the data at each comma
- Dim index = dgv_Books.Rows.Add 'creates a new variable that adds data to the data grid
- dgv_Books.Rows(index).SetValues(Decryption(Fields(0)), Decryption(Fields(1)), Decryption(Fields(2)), Decryption(Fields(3)), Decryption(Fields(4)))
- 'adds values to the datagrid for each field in the record, and also decrypts them so that they can be viewed
- End While
- sr.Close() 'closes streamreader to avoid data loss
- dgv_Books.RowHeadersVisible = False 'turns off the row headers
- dgv_Books.ClearSelection() 'unselects any highlighted rows in the datagrid
- End Sub
- Private Sub Books_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 'when the books form loads, the data grid will be refreshed by calling the subroutine
- DataGridRefresh()
- End Sub
- Private Sub pbx_ExitButton_Click(sender As Object, e As EventArgs) Handles pbx_ExitButton.Click
- 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
- 'when the exit button is clicked, exit the form if yes is selected
- Dim menu As New LibraryMenu 'creates new instance of the menu form
- menu.Show() 'shows the menu form
- Me.Close() 'closes the currrent form
- End If
- End Sub
- Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
- Dim validation As New Validation
- 'if every textbox has no data inputed / presence check
- If txt_BookID.Text = "" Or txt_Name.Text = "" Or txt_ISBN.Text = "" Or txt_Genre.Text = "" Or txt_AuthorID.Text = "" Then
- MsgBox("Please input data into all textboxes") 'shows message to ask user to input data into texboxes
- txt_BookID.Clear() 'clears textboxes
- txt_Name.Clear()
- txt_ISBN.Clear()
- txt_Genre.Clear()
- txt_AuthorID.Clear()
- ElseIf validation.lengthCheck(txt_ISBN.Text, 13, "ISBN", "Limit") = False Then
- txt_ISBN.Clear()
- Else 'if there is data
- Dim sw As New StreamWriter(BookData, True) 'opens new streamwriter
- 'adds the information in the textboxes to the textfile using streamwrite, with commas to split it up
- sw.WriteLine(Encryption(txt_BookID.Text) & "," & Encryption(txt_Name.Text) & "," & Encryption(txt_ISBN.Text) & "," & Encryption(txt_Genre.Text) & "," & Encryption(txt_AuthorID.Text))
- sw.Close()
- MsgBox("New file added") 'shows the user that the file has been added to database
- 'clear all data in the textboxes
- txt_BookID.Text = ""
- txt_Name.Text = ""
- txt_ISBN.Text = ""
- txt_Genre.Text = ""
- txt_AuthorID.Text = ""
- DataGridRefresh() 'refreshes data grid
- End If
- End Sub
- Private Sub btn_Search_Click(sender As Object, e As EventArgs) Handles btn_Search.Click
- Dim i As Integer 'creates new integer variable to be used in a loop
- If txt_BookID.Text = "" Then 'if there is no data inputted into the bookid textbox
- MsgBox("Please input a memberID to search in the textbox and try again.") 'prompts the user to enter data
- Else 'when there is data in the bookid textbox
- For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
- 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:
- dgv_Books.Rows(i).Selected = True 'highlight the currrently selected row
- 'make each textbox show the corresponding field from the data grid:
- txt_BookID.Text = dgv_Books.Rows(i).Cells(0).Value
- txt_Name.Text = dgv_Books.Rows(i).Cells(1).Value
- txt_ISBN.Text = dgv_Books.Rows(i).Cells(2).Value
- txt_Genre.Text = dgv_Books.Rows(i).Cells(3).Value
- txt_AuthorID.Text = dgv_Books.Rows(i).Cells(4).Value
- End If
- Next
- DataGridRefresh() 'refreshes data grid
- dgv_Books.Rows(txt_BookID.Text - 1).Selected = True 'highlights the row of the data grid view that matches the bookID
- 'bookid.text - 1 because the data grid starts with 0
- End If
- End Sub
- Private Sub btn_Edit_Click(sender As Object, e As EventArgs) Handles btn_Edit.Click
- 'shows the selected record in the textboxes to be edited
- txt_BookID.Text = dgv_Books.Rows(RowIndex).Cells(0).Value 'each textbox shows the data from the corresponding field in the record currently selected
- txt_Name.Text = dgv_Books.Rows(RowIndex).Cells(1).Value
- txt_ISBN.Text = dgv_Books.Rows(RowIndex).Cells(2).Value
- txt_Genre.Text = dgv_Books.Rows(RowIndex).Cells(3).Value
- txt_AuthorID.Text = dgv_Books.Rows(RowIndex).Cells(4).Value
- 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
- End Sub
- Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
- Dim fileLines() As String 'creates a new string array to store each line in the text file
- Dim i As Integer 'creates new integer variable to be used in a loop
- Dim fieldSplit() As String 'creates an array to store the data in each field of a record
- fileLines = File.ReadAllLines(BookData) 'saves all of the data in the textfile to the constant
- 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
- 'if yes was clicked in the messsage box:
- If txt_BookID.Text = "" Then 'if the memberid textbox is empty then:
- 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
- Else
- Dim sw As New StreamWriter(BookData, False) 'opens new streamwriter using false instead of true in order to delete data
- For i = 0 To fileLines.Length - 1 'starts new loop for every line in the textfile
- fieldSplit = fileLines(i).Split(",") 'makes the fieldsplit array store each field in the current record by splitting the record at each comma
- If txt_BookID.Text <> dgv_Books.Rows(i).Cells(0).Value AndAlso fieldSplit(i).Length > 0 Then
- '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:
- 'the data will be written back to the textfile
- 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))
- End If
- 'if the bookid in the textbox does match the currently selected record, it will be skipped from being re-written to the textfile
- Next
- sw.Close() 'closes the streamwriter to avoid data loss
- DataGridRefresh() 'refreshes the data grid
- End If
- Else 'if the user choose "no" to continuing to delete the file, then:
- MsgBox("Data will not be deleted") 'shows a message box to confirm that the data will not be deleted
- End If
- End Sub
- Private Sub btn_up_Click(sender As Object, e As EventArgs) Handles btn_up.Click
- If RowIndex > 0 Then 'if the current row selected is more than the first row
- RowIndex = RowIndex - 1 'decrease the row index by 1 which moves to the previous row
- Else
- MsgBox("The first record is selected") 'gives the user a message that tells them that the first row is already selected
- End If
- dgv_Books.ClearSelection() 'this clears which row is highlighted
- dgv_Books.Rows(RowIndex).Selected = True 'this highlights the currently selected row
- End Sub
- Private Sub btn_Down_Click(sender As Object, e As EventArgs) Handles btn_Down.Click
- If RowIndex < LineCount - 1 Then 'if the current row selected is less than the number of rows then:
- RowIndex = RowIndex + 1 'increase index of the row by 1, which moves to the next row
- Else
- MsgBox("The last record is selected") 'this gives a message to the user that they alrady have the last record selected
- End If
- dgv_Books.ClearSelection() 'this clears which row is highlighted
- dgv_Books.Rows(RowIndex).Selected = True 'this highlights the currently selected row
- End Sub
- Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
- Dim fileLines() As String 'creates a new string array to store each line in the text file
- Dim i As Integer 'creates new integer variable to be used in a loop
- Dim fieldSplit() As String 'creates an array to store the data in each field of a record
- fileLines = File.ReadAllLines(BookData) 'saves all of the data in the textfile to the variable
- For i = 0 To fileLines.Length - 1 'starts loop to read all of the lines
- fieldSplit = fileLines(i).Split(",") 'splits each record into each field using the commas
- If fieldSplit.Length > 0 AndAlso Decryption(fieldSplit(0)) = txt_BookID.Text Then 'when there is data in the textfile, and the bookID matches:
- 'changes each field to match the new data in the textboxes
- 'The data is also encrypted as the data in the datagrid view is decrypted, so it must be encrypted to be written
- fieldSplit(0) = Encryption(txt_BookID.Text)
- fieldSplit(1) = Encryption(txt_Name.Text)
- fieldSplit(2) = Encryption(txt_ISBN.Text)
- fieldSplit(3) = Encryption(txt_Genre.Text)
- fieldSplit(4) = Encryption(txt_AuthorID.Text)
- fileLines(i) = String.Join(",", fieldSplit) 'joins all data in each line back together with commas to split it
- End If
- Next
- File.WriteAllLines(BookData, fileLines) 'writes all the data back to the textfile
- DataGridRefresh() 'refreshes datgrid
- End Sub
- Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
- 'clears all textboxes
- txt_BookID.Clear()
- txt_Name.Clear()
- txt_ISBN.Clear()
- txt_Genre.Clear()
- txt_AuthorID.Clear()
- DataGridRefresh() 'refreshes data grid
- End Sub
- Private Sub btn_checkout_Click(sender As Object, e As EventArgs) Handles btn_checkout.Click
- Dim checkout As New Checkout 'creates new instance of the checkout form
- checkout.Show() 'shows the checkout form
- Me.Close() 'closes the currrent form
- End Sub
- Private Sub btn_author_Click(sender As Object, e As EventArgs) Handles btn_author.Click
- Dim author As New Author 'creates new instance of the author form
- author.Show() 'shows the author form
- Me.Close() 'closes the currrent form
- End Sub
- Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
- Dim i As Integer 'creates an integer variable to be used as a counter
- Dim sortingArray(4) As String 'creates an array to store temporary data in order to swap the data in other arrays
- Dim currentRecord(4) As String 'creates an array to store the data in the currently viewed record in the loop
- Dim nextRecord(4) As String 'creates an array to store the data in the next record after the current record in the loop
- Dim sw1 As New StreamWriter(BookData, False) 'deletes the data in the textfile
- sw1.Close() 'closes streamwriter
- For i = 0 To LineCount - 1 'starts a loop to view every line in the textfile
- 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)
- 'makes the currentrecord array store every field of the current record
- currentRecord(0) = dgv_Books.Rows(counter).Cells(0).Value
- currentRecord(1) = dgv_Books.Rows(counter).Cells(1).Value
- currentRecord(2) = dgv_Books.Rows(counter).Cells(2).Value
- currentRecord(3) = dgv_Books.Rows(counter).Cells(3).Value
- currentRecord(4) = dgv_Books.Rows(counter).Cells(4).Value
- 'makes the nextrecord array store every field in the next record
- nextRecord(0) = dgv_Books.Rows(counter + 1).Cells(0).Value
- nextRecord(1) = dgv_Books.Rows(counter + 1).Cells(1).Value
- nextRecord(2) = dgv_Books.Rows(counter + 1).Cells(2).Value
- nextRecord(3) = dgv_Books.Rows(counter + 1).Cells(3).Value
- nextRecord(4) = dgv_Books.Rows(counter + 1).Cells(4).Value
- If currentRecord(0) > nextRecord(0) Then 'if the first field in the current record is larger than the first field in the next array:
- sortingArray = currentRecord 'makes the temporary sorting array store the data in the current record
- currentRecord = nextRecord 'the current record will then store the data from the next record
- nextRecord = sortingArray 'the next record will store the data from the current record
- 'this is essentially a swap of the data in the two arrays
- End If
- dgv_Books.Rows(counter).SetValues(currentRecord) 'sets the currently viewed row in the datagrid to the data in the currentrecord array
- dgv_Books.Rows(counter + 1).SetValues(nextRecord) 'sets the next row in the datagrid to the data in the nextrecord array
- Next
- Dim sw As New StreamWriter(BookData, True) 'opens new streamwriter to write data to the textfi;e
- 'adds each sorted line in the datagrid to the textfile using the loop
- 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))
- sw.Close() 'closes streamwriter to avoid data loss
- Next
- End Sub
- Private Sub btn_Help_Click(sender As Object, e As EventArgs) Handles btn_Help.Click
- 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. " _
- & "You can navigate this by using either the arrow buttons underneath, or by searching for a specific bookID. " _
- & "To search, you need to input a bookID into the bookID textbox, then click the search button. To add data to the database, " _
- & "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. " _
- & "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. " _
- & "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. " _
- & "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. " _
- & "If you want to clear the textboxes of unnecessary data, you can click the clear textboxes button to do this. " _
- & "If the data is in an incorrect order (it should be chronological by bookID), click the sort button to sort the data. " _
- & "To view either the author form or the checkout form, click the respective buttons. " _
- & "Finally, to exit back to the menu form, you can click the cross button in the top right of the form."
- Help.Show()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement