ElliottE4

Untitled

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