Advertisement
NAK

UCAS Calculator

NAK
Apr 28th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.21 KB | None | 0 0
  1. Public Class Form1
  2.     Public PassTotal = 0
  3.  
  4.     ' Enum Used in all the comboBoxes to provide the user with a choice
  5.     Public Enum Achieved
  6.         Fail = 0
  7.         Pass = 70
  8.         Merit = 80
  9.         Distinction = 90
  10.     End Enum
  11.  
  12.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  13.  
  14.         ' OK so lets set up all our comboBoxes
  15.         SetUpComboBoxes(Me)
  16.  
  17.     End Sub
  18.  
  19.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  20.  
  21.         ' this is where we are going to do all the work
  22.         PassTotal = 0 ' reset the initial value of Total Score to zero]
  23.         ' sum up all the selected values...
  24.         TextBox1.Text = CalculatePassTotal(Me) ' remember the enum at the top [Achieved]... we are summing those values that have been selected
  25.  
  26.         ' we have the total so get the Grade
  27.         CalculateOverallGrade(PassTotal)
  28.  
  29.         ' now display it
  30.         TextBox2.Text = Grade.OverallGradeProperty
  31.         TextBox3.Text = Grade.GradePointsProperty
  32.         TextBox4.Text = Val(Grade.UcasPointsProperty)
  33.  
  34.     End Sub
  35.  
  36.     Public Function CalculatePassTotal(ByVal ThisForm As Control)
  37.         ' again, loop though all the controls on the form
  38.         For Each ctrl As Control In ThisForm.Controls
  39.             If TypeOf ctrl Is ComboBox Then ' if this control is a ComboBox
  40.                 PassTotal += CType(ctrl, ComboBox).SelectedValue ' add the selected value [Achieved Enum] to our running total [PassTotal]
  41.             End If
  42.         Next ctrl
  43.  
  44.         Return PassTotal ' return PassTotal
  45.  
  46.     End Function
  47.  
  48.     Public Sub CalculateOverallGrade(pt As Integer) 'As Grade
  49.  
  50.         'now use PassTotal value to determine the grade (check these values, the first few looked a bit dodgy so i changed them)
  51.         Select Case pt
  52.             Case Is < 1260
  53.                 Grade.OverallGradeProperty = "Ungraded"
  54.                 Grade.GradePointsProperty = "0"
  55.                 Grade.UcasPointsProperty = 0
  56.             Case 1260 To 1300
  57.                 Grade.OverallGradeProperty = "PPP"
  58.                 Grade.GradePointsProperty = "108-131"
  59.                 Grade.UcasPointsProperty = 120
  60.             Case 1301 To 1339
  61.                 Grade.OverallGradeProperty = "MPP"
  62.                 Grade.GradePointsProperty = "132-155"
  63.                 Grade.UcasPointsProperty = 160
  64.             Case 1340 To 1379
  65.                 Grade.OverallGradeProperty = "MMP"
  66.                 Grade.GradePointsProperty = "156-179"
  67.                 Grade.UcasPointsProperty = 200
  68.             Case 1380 To 1419
  69.                 Grade.OverallGradeProperty = "MMM"
  70.                 Grade.GradePointsProperty = "180-203"
  71.                 Grade.UcasPointsProperty = 240
  72.             Case 1420 To 1459
  73.                 Grade.OverallGradeProperty = "DMM"
  74.                 Grade.GradePointsProperty = "204-227"
  75.                 Grade.UcasPointsProperty = 280
  76.             Case 1460 To 1499
  77.                 Grade.OverallGradeProperty = "DDM"
  78.                 Grade.GradePointsProperty = "228-251"
  79.                 Grade.UcasPointsProperty = 320
  80.             Case 1500 To 1529
  81.                 Grade.OverallGradeProperty = "DDD"
  82.                 Grade.GradePointsProperty = "252-275"
  83.                 Grade.UcasPointsProperty = 360
  84.             Case 1530 To 1559
  85.                 Grade.OverallGradeProperty = "D*DD"
  86.                 Grade.GradePointsProperty = "276-299"
  87.                 Grade.UcasPointsProperty = 380
  88.             Case 1560 To 1589
  89.                 Grade.OverallGradeProperty = "D*D*D"
  90.                 Grade.GradePointsProperty = "300-323"
  91.                 Grade.UcasPointsProperty = 400
  92.             Case Is >= 1590
  93.                 Grade.OverallGradeProperty = "D*D*D*"
  94.                 Grade.GradePointsProperty = "324-347"
  95.                 Grade.UcasPointsProperty = 420
  96.             Case Else
  97.  
  98.         End Select
  99.  
  100.     End Sub
  101.  
  102.     Public Sub SetUpComboBoxes(ByVal ThisForm As Control)
  103.  
  104.         ' this function loops round all of the controls on this form and sets some properties for each of our 18 ComboBoxes
  105.         For Each ctrl As Control In ThisForm.Controls
  106.             If TypeOf ctrl Is ComboBox Then
  107.                 ' This is cool, we are pointing the combobox at an ENUM [Achieved] for its data source
  108.                 CType(ctrl, ComboBox).DataSource = System.Enum.GetValues(GetType(Achieved)) ' no need to add each item individually just point and go!!
  109.                 CType(ctrl, ComboBox).SelectedIndex = 0 ' set the initial selected item to 0 (Fail)
  110.                 ' OK, this next line is a delegate... what it is saying is this... whenever the SelectedValueChanged changes in ANY of the
  111.                 ' ComboBoxes call the ClearTextBoxes Sub
  112.                 AddHandler CType(ctrl, ComboBox).SelectedValueChanged, AddressOf ClearTextBoxes ' you can take this line out if you don't want to use it
  113.                 My.Settings.Save()
  114.             End If
  115.         Next ctrl
  116.  
  117.     End Sub
  118.  
  119.     Private Sub ClearTextBoxes(sender As Object, e As EventArgs)
  120.         ' this function loops round all of the controls on this form and clears all the TextBoxes
  121.         For Each ctrl As Control In Me.Controls
  122.             If TypeOf ctrl Is TextBox Then
  123.                 CType(ctrl, TextBox).Text = "" ' set the initialy selected item to -1 (nothing)
  124.             End If
  125.         Next ctrl
  126.     End Sub
  127.  
  128. End Class
  129.  
  130. Public Module Grade
  131.  
  132.     ' this is a simple Module to represent a single grade. Straight forward really
  133.     Private GradePoints As String
  134.     Public Property GradePointsProperty() As String
  135.         Get
  136.             Return GradePoints
  137.         End Get
  138.         Set(ByVal value As String)
  139.             GradePoints = value
  140.         End Set
  141.     End Property
  142.  
  143.     Private OverallGrade As String
  144.     Public Property OverallGradeProperty() As String
  145.         Get
  146.             Return OverallGrade
  147.         End Get
  148.         Set(ByVal value As String)
  149.             OverallGrade = value
  150.         End Set
  151.     End Property
  152.  
  153.     Private UcasPoints As Integer
  154.     Public Property UcasPointsProperty() As Integer
  155.         Get
  156.             Return UcasPoints
  157.         End Get
  158.         Set(ByVal value As Integer)
  159.             UcasPoints = value
  160.         End Set
  161.     End Property
  162.  
  163. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement