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 Staff
- Dim StaffData 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
- StaffData = Application.StartupPath & "\staff.txt" 'sets the staffData variable equal to the file path of the staff text file
- If Dir(StaffData) = "" Then 'checks if the staff 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(StaffData, 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_Staff.Rows.Clear() 'clears the data grid
- LineCount = File.ReadAllLines(StaffData).Length 'makes the linecount equal to the number of lines in the textfile
- Dim sr As New StreamReader(StaffData) '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_Staff.Rows.Add 'creates a new variable that adds data to the data grid
- 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)))
- '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_Staff.RowHeadersVisible = False 'turns off the row headers
- dgv_Staff.ClearSelection() 'unselects any highlighted rows in the datagrid
- End Sub
- Private Sub Staff_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- If Security = 1 Then
- btn_Add.Visible = False
- btn_Edit.Visible = False
- btn_Delete.Visible = False
- btn_save.Visible = False
- btn_Search.Visible = False
- btn_Sort.Visible = False
- dgv_Staff.Visible = False
- End If
- 'when the staff 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 UniqueUsername As Boolean = True 'creates a new boolean variable that is used to chek if the username added is unique
- Dim validation As New Validation
- For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
- 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:
- UniqueUsername = False
- End If
- Next
- 'if every textbox has no data inputed / presence check
- 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
- MsgBox("Please input data into all textboxes") 'shows message to ask user to input data into texboxes
- txt_StaffID.Clear() 'clears textboxes
- txt_Username.Clear()
- txt_Password.Clear()
- txt_Address.Clear()
- txt_Tel.Clear()
- txt_AccessLevel.Clear()
- txt_SecurityQuestion.Clear()
- txt_SecurityAnswer.Clear()
- ElseIf validation.lengthCheck(txt_Tel.Text, 11, "Telephone number", "Limit") = False Then
- txt_Tel.Clear()
- ElseIf UniqueUsername = False Then
- MsgBox("Username already exists. Please use another username") 'prompts the user to use a unique username
- txt_Username.Clear()
- Else 'if there is data
- Dim sw As New StreamWriter(StaffData, 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_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))
- sw.Close()
- MsgBox("New file added") 'shows the user that the file has been added to database
- 'clear all data in the textboxes
- txt_StaffID.Text = ""
- txt_Username.Text = ""
- txt_Password.Text = ""
- txt_Address.Text = ""
- txt_Tel.Text = ""
- txt_AccessLevel.Text = ""
- txt_SecurityQuestion.Text = ""
- txt_SecurityAnswer.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_StaffID.Text = "" Then 'if there is no data inputted into the staffid textbox
- MsgBox("Please input a staffID to search in the textbox and try again.") 'prompts the user to enter data
- Else 'when there is data in the staffid textbox
- For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
- 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:
- dgv_Staff.Rows(i).Selected = True 'highlight the currrently selected row
- 'make each textbox show the corresponding field from the data grid:
- txt_StaffID.Text = dgv_Staff.Rows(i).Cells(0).Value
- txt_Username.Text = dgv_Staff.Rows(i).Cells(1).Value
- txt_Password.Text = dgv_Staff.Rows(i).Cells(2).Value
- txt_Address.Text = dgv_Staff.Rows(i).Cells(3).Value
- txt_Tel.Text = dgv_Staff.Rows(i).Cells(4).Value
- txt_AccessLevel.Text = dgv_Staff.Rows(i).Cells(5).Value
- txt_SecurityQuestion.Text = dgv_Staff.Rows(i).Cells(6).Value
- txt_SecurityAnswer.Text = dgv_Staff.Rows(i).Cells(7).Value
- End If
- Next
- DataGridRefresh() 'refreshes data grid
- dgv_Staff.Rows(txt_StaffID.Text - 1).Selected = True 'highlights the row of the data grid view that matches the staffID
- 'staffid.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_StaffID.Text = dgv_Staff.Rows(RowIndex).Cells(0).Value 'each textbox shows the data from the corresponding field in the record currently selected
- txt_Username.Text = dgv_Staff.Rows(RowIndex).Cells(1).Value
- txt_Password.Text = dgv_Staff.Rows(RowIndex).Cells(2).Value
- txt_Address.Text = dgv_Staff.Rows(RowIndex).Cells(3).Value
- txt_Tel.Text = dgv_Staff.Rows(RowIndex).Cells(4).Value
- txt_AccessLevel.Text = dgv_Staff.Rows(RowIndex).Cells(5).Value
- txt_SecurityQuestion.Text = dgv_Staff.Rows(RowIndex).Cells(6).Value
- txt_SecurityAnswer.Text = dgv_Staff.Rows(RowIndex).Cells(7).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_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(StaffData) '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_StaffID.Text Then 'when there is data in the textfile, and the staffID 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_StaffID.Text)
- fieldSplit(1) = Encryption(txt_Username.Text)
- fieldSplit(2) = Encryption(txt_Password.Text)
- fieldSplit(3) = Encryption(txt_Address.Text)
- fieldSplit(4) = Encryption(txt_Tel.Text)
- fieldSplit(5) = Encryption(txt_AccessLevel.Text)
- fieldSplit(6) = Encryption(txt_SecurityQuestion.Text)
- fieldSplit(7) = Encryption(txt_SecurityAnswer.Text)
- fileLines(i) = String.Join(",", fieldSplit) 'joins all data in each line back together with commas to split it
- End If
- Next
- File.WriteAllLines(StaffData, fileLines) 'writes all the data back to the textfile
- DataGridRefresh() 'refreshes datgrid
- 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(StaffData) 'saves all of the data in the textfile to the variable
- 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_StaffID.Text = "" Then 'if the staffid textbox is empty then:
- 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
- Else
- Dim sw As New StreamWriter(StaffData, 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_StaffID.Text <> dgv_Staff.Rows(i).Cells(0).Value AndAlso fieldSplit(i).Length > 0 Then
- '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:
- 'the data will be written back to the textfile
- 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))
- End If
- 'if the staffid 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_Staff.ClearSelection() 'this clears which row is highlighted
- dgv_Staff.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_Staff.ClearSelection() 'this clears which row is highlighted
- dgv_Staff.Rows(RowIndex).Selected = True 'this highlights the currently selected row
- End Sub
- Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
- 'clears all textboxes
- txt_StaffID.Clear()
- txt_Username.Clear()
- txt_Password.Clear()
- txt_Address.Clear()
- txt_Tel.Clear()
- txt_AccessLevel.Clear()
- txt_SecurityQuestion.Clear()
- txt_SecurityAnswer.Clear()
- DataGridRefresh() 'refreshes data grid
- End Sub
- Private Sub btn_timetable_Click(sender As Object, e As EventArgs) Handles btn_timetable.Click
- Dim timetable As New Timetable 'creates new instance of the timetable form
- timetable.Show() 'shows the timetable 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(7) As String 'creates an array to store temporary data in order to swap the data in other arrays
- Dim currentRecord(7) As String 'creates an array to store the data in the currently viewed record in the loop
- Dim nextRecord(7) 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(StaffData, 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_Staff.Rows(counter).Cells(0).Value
- currentRecord(1) = dgv_Staff.Rows(counter).Cells(1).Value
- currentRecord(2) = dgv_Staff.Rows(counter).Cells(2).Value
- currentRecord(3) = dgv_Staff.Rows(counter).Cells(3).Value
- currentRecord(4) = dgv_Staff.Rows(counter).Cells(4).Value
- currentRecord(5) = dgv_Staff.Rows(counter).Cells(5).Value
- currentRecord(6) = dgv_Staff.Rows(counter).Cells(6).Value
- currentRecord(7) = dgv_Staff.Rows(counter).Cells(7).Value
- 'makes the nextrecord array store every field in the next record
- nextRecord(0) = dgv_Staff.Rows(counter + 1).Cells(0).Value
- nextRecord(1) = dgv_Staff.Rows(counter + 1).Cells(1).Value
- nextRecord(2) = dgv_Staff.Rows(counter + 1).Cells(2).Value
- nextRecord(3) = dgv_Staff.Rows(counter + 1).Cells(3).Value
- nextRecord(4) = dgv_Staff.Rows(counter + 1).Cells(4).Value
- nextRecord(5) = dgv_Staff.Rows(counter + 1).Cells(5).Value
- nextRecord(6) = dgv_Staff.Rows(counter + 1).Cells(6).Value
- nextRecord(7) = dgv_Staff.Rows(counter + 1).Cells(7).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_Staff.Rows(counter).SetValues(currentRecord) 'sets the currently viewed row in the datagrid to the data in the currentrecord array
- dgv_Staff.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(StaffData, 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_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))
- 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 staff form. On the right of the form is a data grid viewm, which displays the staff database. " _
- & "You can navigate this by using either the arrow buttons underneath, or by searching for a specific staffID. " _
- & "To search, you need to input a staffID into the staffID 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 staffID of the record you wish to delete into the staffID 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 staffID), click the sort button to sort the data. " _
- & "To view the staff timetable, click the timetable button. " _
- & "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