Advertisement
willeds

Untitled

Apr 5th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.06 KB | None | 0 0
  1. Imports System.IO
  2. Public Class mySupplier
  3.     Private Sub mySupplier_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.         fillGrid(dgv_supplier, fileLoc_Supplier)
  5.         StartPosition = FormStartPosition.CenterScreen
  6.  
  7.         txt_supplierID.BackColor = gray
  8.         txt_supplierName.BackColor = gray
  9.         txt_nOPS.BackColor = gray
  10.     End Sub
  11.  
  12.     Private Sub fillText(ByVal rowIndex As Integer)
  13.         If dgv_supplier.Rows.Count = 0 Then
  14.             Return
  15.         End If
  16.         txt_supplierID.Text = dgv_supplier.Rows(rowIndex).Cells(0).Value
  17.         txt_supplierName.Text = dgv_supplier.Rows(rowIndex).Cells(1).Value
  18.         txt_nOPS.Text = dgv_supplier.Rows(rowIndex).Cells(2).Value
  19.     End Sub
  20.  
  21.     Private Sub clearText()
  22.         txt_supplierID.Text = ""
  23.         txt_supplierName.Text = ""
  24.         txt_nOPS.Text = ""
  25.     End Sub
  26.  
  27.     'Navigation
  28.     Private dgvNavigator As New DataGridViewNavigator()
  29.     Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
  30.         dgvNavigator.SelectFirst(dgv_supplier)
  31.         fillText(dgvNavigator.CurrentIndex)
  32.     End Sub
  33.  
  34.     Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
  35.         dgvNavigator.SelectPrevious(dgv_supplier)
  36.         fillText(dgvNavigator.CurrentIndex)
  37.     End Sub
  38.  
  39.     Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
  40.         dgvNavigator.SelectNext(dgv_supplier)
  41.         fillText(dgvNavigator.CurrentIndex)
  42.     End Sub
  43.  
  44.     Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
  45.         dgvNavigator.SelectLast(dgv_supplier)
  46.         fillText(dgvNavigator.CurrentIndex)
  47.     End Sub
  48.     '----------------------------------------------------------------------------------------------'
  49.     Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
  50.         clearText()
  51.         Dim newID As Integer = AddRecord(fileLoc_Supplier)
  52.         txt_supplierID.Text = newID
  53.     End Sub
  54.  
  55.     Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
  56.         ' Checks before attempting to add to file
  57.         If Check() = False Then
  58.             Return
  59.         End If
  60.  
  61.         Dim supplierID As Integer = txt_supplierID.Text
  62.  
  63.  
  64.         If UniqueCheck(supplierID, fileLoc_Supplier) Then
  65.             Dim newSupplier As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
  66.             SaveRecord(fileLoc_Supplier, newSupplier)
  67.             fillGrid(dgv_supplier, fileLoc_Supplier)
  68.         Else
  69.             MsgBox("ID is not unique.")
  70.         End If
  71.  
  72.     End Sub
  73.  
  74.  
  75.     Private Function Check()
  76.  
  77.         'Presence check on the supplier ID
  78.         If txt_supplierID.Text = "" Then
  79.             MsgBox("please enter a supplier ID")
  80.             Return False
  81.         End If
  82.  
  83.         ' Integer check on the ID
  84.         If Not IntCheck(txt_supplierID.Text) Then
  85.             Return False
  86.         End If
  87.  
  88.         ' Length check (and presence) on the length of the supplier
  89.         If txt_supplierName.Text.Length < 3 Or txt_supplierName.Text.Length > 20 Then
  90.             MsgBox("Invalid Supplier name, must be between 3 and 20 characters")
  91.             Return False
  92.         End If
  93.  
  94.         ' Number of products supplied can either be blank or an integer
  95.         If Not txt_nOPS.Text = "" Then
  96.             If Not IntCheck(txt_nOPS.Text) Then
  97.                 Return False
  98.             End If
  99.         End If
  100.  
  101.         Return True
  102.  
  103.     End Function
  104.  
  105.  
  106.  
  107.  
  108.     Private Sub btn_edit_Click(sender As Object, e As EventArgs) Handles btn_edit.Click
  109.  
  110.         If Check() = False Then
  111.             Return
  112.         End If
  113.         ' Checks data to ensure validity
  114.         Dim ID As Integer = txt_supplierID.Text
  115.         Dim newValues As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
  116.         EditRecord(ID, newValues, fileLoc_Supplier)
  117.         fillGrid(dgv_supplier, fileLoc_Supplier)
  118.  
  119.  
  120.     End Sub
  121.  
  122.     Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
  123.  
  124.         Dim supplierID As Integer = txt_supplierID.Text
  125.  
  126.  
  127.         'Declares an array called lines, which contains every line of from the text file
  128.         Dim lines As String() = File.ReadAllLines(fileLoc_Product)
  129.         Dim productIDs As New List(Of String)
  130.  
  131.         Dim NOPS As Integer = 0
  132.  
  133.         ' Does not allow a supplier to be deleted if they supply products in the system
  134.         ' Does this by
  135.  
  136.         ' Splits product records by the comma
  137.         ' Checks part 1 (supplier ID)
  138.         ' if exists and is integer, compares to the supplier that is attempting to be deleted
  139.         ' IF MATCH
  140.         ' Increment Number of products supplied
  141.         ' Add product ID to a list
  142.         ' IF NOT MATCH
  143.         ' Check next product
  144.         '
  145.         ' Done until checked every product
  146.  
  147.         ' THEN
  148.         ' either no products are supplied, the supplier is deleted
  149.         ' OR
  150.         ' error message telling user how many products are supplied, which ID's are supplied by the supplier
  151.         For Each line As String In lines
  152.             Dim parts As String() = line.Split(","c)
  153.             If Integer.TryParse(parts(1), Nothing) AndAlso Integer.Parse(parts(1)) = supplierID Then
  154.                 NOPS = NOPS + 1
  155.                 productIDs.Add(parts(0))
  156.             End If
  157.         Next
  158.  
  159.         If NOPS = 0 Then
  160.             DeleteRecord(dgv_supplier, fileLoc_Supplier, supplierID)
  161.             fillGrid(dgv_supplier, fileLoc_Supplier)
  162.         Else
  163.             MsgBox("This supplier supplies " & NOPS & " products, you must remove these products before deleting the supplier:" & String.Join(", ", productIDs))
  164.         End If
  165.  
  166.     End Sub
  167.  
  168.     Private Sub btn_nOPS_Click(sender As Object, e As EventArgs) Handles btn_nOPS.Click
  169.         Check()
  170.         Dim supplierID As Integer = txt_supplierID.Text
  171.  
  172.  
  173.         'Declares an array called lines, which contains every line of from the text file
  174.         Dim lines As String() = File.ReadAllLines(fileLoc_Product)
  175.  
  176.  
  177.         Dim NOPS As Integer = 0
  178.  
  179.         ' Checks each product in product file
  180.         ' Splits by ,
  181.         ' If supplier ID field corresponds to the queried supplier
  182.         ' Increment Number of Products Supplied (NOPS)
  183.         ' Edit record to display the NOPS
  184.  
  185.         For Each line As String In lines
  186.             Dim parts As String() = line.Split(","c)
  187.             If Integer.TryParse(parts(1), Nothing) AndAlso Integer.Parse(parts(1)) = supplierID Then
  188.                 NOPS = NOPS + 1
  189.             End If
  190.         Next
  191.  
  192.         txt_nOPS.Text = NOPS
  193.  
  194.         Dim ID As Integer = txt_supplierID.Text
  195.         Dim newValues As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
  196.         EditRecord(ID, newValues, fileLoc_Supplier)
  197.         fillGrid(dgv_supplier, fileLoc_Supplier)
  198.  
  199.     End Sub
  200.     'Takes user to main menu
  201.     Private Sub btn_menu_Click(sender As Object, e As EventArgs) Handles btn_menu.Click
  202.         Me.Visible = False
  203.         myMenu.Visible = True
  204.     End Sub
  205.  
  206.     'Sorts
  207.  
  208.     ' Supplier ID Ascending
  209.     ' Supplier ID Descending
  210.  
  211.     ' Number of products supplied Ascending
  212.     ' Number of products supplied Descending
  213.  
  214.  
  215.     Private Sub btn_suppIdAsc_Click(sender As Object, e As EventArgs) Handles btn_suppIdAsc.Click
  216.         AscSort(fileLoc_Supplier, 0)
  217.         fillGrid(dgv_supplier, fileLoc_Supplier)
  218.     End Sub
  219.  
  220.     Private Sub btn_suppIdDesc_Click(sender As Object, e As EventArgs) Handles btn_suppIdDesc.Click
  221.         DescSort(fileLoc_Supplier, 0)
  222.         fillGrid(dgv_supplier, fileLoc_Supplier)
  223.     End Sub
  224.  
  225.     Private Sub btn_NopsAsc_Click(sender As Object, e As EventArgs) Handles btn_NopsAsc.Click
  226.         AscSort(fileLoc_Supplier, 2)
  227.         fillGrid(dgv_supplier, fileLoc_Supplier)
  228.     End Sub
  229.  
  230.     Private Sub btn_NopsDesc_Click(sender As Object, e As EventArgs) Handles btn_NopsDesc.Click
  231.         DescSort(fileLoc_Supplier, 2)
  232.         fillGrid(dgv_supplier, fileLoc_Supplier)
  233.     End Sub
  234.  
  235.  
  236. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement