1 Public Class Form1 2 Inherits System.Windows.Forms.Form 3 4 ' This application calculates a student's grade point average (GPA). 5 ' It demonstrates how to use Function procedures. 6 7 Dim defaultTextboxColor As Color 8 9 Private Sub Form1_Load(ByVal sender As System.Object, _ 10 ByVal e As System.EventArgs) Handles MyBase.Load 11 12 'Get the default text box color. 13 defaultTextboxColor = txtCreditHours1.BackColor 14 End Sub 15 16 17 Private Sub btnCalculate_Click(ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) Handles btnCalculate.Click 19 20 ' This event procedure displays the quality points for 21 ' each course and the GPA. 22 Dim intGradePoints As Integer 23 Dim sngGradePointAvg As Single 24 25 ' Display the quality points for course 1. 26 intGradePoints = CalcGradePoints(txtGrade1.Text) 27 lblGradePoints1.Text = intGradePoints.ToString() 28 29 ' Display the quality points for course 2. 30 intGradePoints = CalcGradePoints(txtGrade2.Text) 31 lblGradePoints2.Text = intGradePoints.ToString() 32 33 ' Display the quality points for course 3. 34 intGradePoints = CalcGradePoints(txtGrade3.Text) 35 lblGradePoints3.Text = intGradePoints.ToString() 36 37 ' Display the quality points for course 4. 38 intGradePoints = CalcGradePoints(txtGrade4.Text) 39 lblGradePoints4.Text = intGradePoints.ToString() 40 41 ' Get the grade point average and display 42 ' it with 2 decimal places. 43 sngGradePointAvg = CalcGPA() 44 lblGPA.Text = FormatNumber(sngGradePointAvg, 2) 45 46 End Sub 47 48 49 Function CalcGradePoints(ByVal strGrade As String) As Integer 50 51 ' This function accepts a letter grade as an argument 52 ' and returns the number of grade points for that grade. 53 54 Dim intPoints As Integer 55 56 Select Case strGrade.ToUpper() 57 Case "A" 58 intPoints = 4 59 Case "B" 60 intPoints = 3 61 Case "C" 62 intPoints = 2 63 Case "D" 64 intPoints = 1 65 Case "F" 66 intPoints = 0 67 End Select 68 69 ' Return the number of grade points. 70 Return intPoints 71 End Function 72 73 Function CalcGPA() As Single 74 75 ' This function calculates and returns the student's 76 ' Grade Point Average (GPA). 77 78 ' Get the total credit hours. 79 Dim intTotalCreditHours As Integer = _ 80 CInt(txtCreditHours1.Text) + _ 81 CInt(txtCreditHours2.Text) + _ 82 CInt(txtCreditHours3.Text) + _ 83 CInt(txtCreditHours4.Text) 84 85 ' Calculate the total quality points. 86 Dim intTotalQualityPoints As Integer = _ 87 (CInt(txtCreditHours1.Text) * CInt(lblGradePoints1.Text)) + _ 88 (CInt(txtCreditHours2.Text) * CInt(lblGradePoints2.Text)) + _ 89 (CInt(txtCreditHours3.Text) * CInt(lblGradePoints3.Text)) + _ 90 (CInt(txtCreditHours4.Text) * CInt(lblGradePoints4.Text)) 91 92 ' Divide the total quality points by the total credit 93 ' hours, producing the grade point average. 94 Return CSng(intTotalQualityPoints / intTotalCreditHours) 95 End Function 96 97 Function ValidateHours(ByVal tbox As TextBox) As Boolean 98 99 ' Return True if the text box contains a valid integer. 100 ' If not, display an error message, change the TextBox color, 101 ' and return False. 102 103 If IsNumeric(tbox.Text) Then 104 tbox.BackColor = defaultTextboxColor 105 Return True 106 Else 107 lblMessage.Text = "Invalid Credit Hours. Please enter an integer" 108 tbox.BackColor = Color.Aqua 109 Return False 110 End If 111 End Function 112 113 Private Sub txtCreditHours1_Validating(ByVal sender As Object, _ 114 ByVal e As System.ComponentModel.CancelEventArgs) _ 115 Handles txtCreditHours1.Validating 116 117 If ValidateHours(txtCreditHours1) Then 118 e.Cancel = False 119 Else 120 e.Cancel = True 121 End If 122 End Sub 123 124 Private Sub txtCreditHours2_Validating(ByVal sender As Object, _ 125 ByVal e As System.ComponentModel.CancelEventArgs) _ 126 Handles txtCreditHours2.Validating 127 128 If ValidateHours(txtCreditHours2) Then 129 e.Cancel = False 130 Else 131 e.Cancel = True 132 End If 133 End Sub 134 135 Private Sub txtCreditHours3_Validating(ByVal sender As Object, _ 136 ByVal e As System.ComponentModel.CancelEventArgs) _ 137 Handles txtCreditHours3.Validating 138 139 If ValidateHours(txtCreditHours3) Then 140 e.Cancel = False 141 Else 142 e.Cancel = True 143 End If 144 End Sub 145 146 Private Sub txtCreditHours4_Validating(ByVal sender As Object, _ 147 ByVal e As System.ComponentModel.CancelEventArgs) _ 148 Handles txtCreditHours4.Validating 149 150 If ValidateHours(txtCreditHours4) Then 151 e.Cancel = False 152 Else 153 e.Cancel = True 154 End If 155 End Sub 156 157 158 Function ValidateGrade(ByVal tbox As TextBox) As Boolean 159 160 ' Return True if the text box contains a valid grade letter 161 ' (A,B,C,D, or F). If not, display an error message, change 162 ' the TextBox color, and return False. 163 164 Dim grade As String = tbox.Text.ToUpper() 165 166 If grade = "A" Or grade = "B" Or grade = "C" Or grade = "D" _ 167 Or grade = "F" Then 168 tbox.BackColor = defaultTextboxColor 169 Return True 170 Else 171 lblMessage.Text = "Invalid Grade. Please enter A, B, C, D or F" 172 tbox.BackColor = Color.Aqua 173 Return False 174 End If 175 176 End Function 177 178 Private Sub txtGrade1_Validating(ByVal sender As Object, _ 179 ByVal e As System.ComponentModel.CancelEventArgs) _ 180 Handles txtGrade1.Validating 181 182 If ValidateGrade(txtGrade1) Then 183 e.Cancel = False 184 Else 185 e.Cancel = True 186 End If 187 End Sub 188 189 Private Sub txtGrade2_Validating(ByVal sender As Object, _ 190 ByVal e As System.ComponentModel.CancelEventArgs) _ 191 Handles txtGrade2.Validating 192 193 If ValidateGrade(txtGrade2) Then 194 e.Cancel = False 195 Else 196 e.Cancel = True 197 End If 198 End Sub 199 200 Private Sub txtGrade3_Validating(ByVal sender As Object, _ 201 ByVal e As System.ComponentModel.CancelEventArgs) _ 202 Handles txtGrade3.Validating 203 204 If ValidateGrade(txtGrade3) Then 205 e.Cancel = False 206 Else 207 e.Cancel = True 208 End If 209 End Sub 210 211 Private Sub txtGrade4_Validating(ByVal sender As Object, _ 212 ByVal e As System.ComponentModel.CancelEventArgs) _ 213 Handles txtGrade4.Validating 214 215 If ValidateGrade(txtGrade4) Then 216 e.Cancel = False 217 Else 218 e.Cancel = True 219 End If 220 End Sub 221 222 Private Sub btnClear_Click(ByVal sender As System.Object, _ 223 ByVal e As System.EventArgs) Handles btnClear.Click 224 225 ' Clear the text boxes 226 txtGrade1.Clear() 227 txtGrade2.Clear() 228 txtGrade3.Clear() 229 txtGrade4.Clear() 230 txtCreditHours1.Clear() 231 txtCreditHours2.Clear() 232 txtCreditHours3.Clear() 233 txtCreditHours4.Clear() 234 ' Clear the labels 235 lblGradePoints1.Text = "" 236 lblGradePoints2.Text = "" 237 lblGradePoints3.Text = "" 238 lblGradePoints4.Text = "" 239 lblGPA.Text = "" 240 End Sub 241 242 Private Sub btnExit_Click(ByVal sender As System.Object, _ 243 ByVal e As System.EventArgs) Handles btnExit.Click 244 245 ' End the application 246 Me.Close() 247 End Sub 248 249 End Class