Private mblngrade As Boolean
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
'declare variables
Dim decTest1, decTest2, decTest3, decAverage As Decimal
Dim strID, strName, strGrade As String
Dim blnGrade As Boolean
'assign value to variables
decTest1 = Val(Me.Test1TextBox.Text)
decTest2 = Val(Me.Test2TextBox.Text)
decTest3 = Val(Me.Test3TextBox.Text)
strID = Me.IDTextBox.Text
strName = Me.StudentTextBox.Text
mblnGrade = ValidGrade(decTest1, decTest2, decTest3)
decAverage = (decTest1 + decTest2 + decTest3) / 3
'display Grade
If blnGrade = True Then
Me.AverageDisplay.Text = decAverage
Else
MessageBox.Show("This value must be between 0 and 100.", "Average", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
'delcare varaible for boolean value
Dim blnGrade As Boolean
If dec1 >= 0 And dec1 <= 100 Then
If dec2 >= 0 And dec2 <= 100 Then
If dec3 >= 0 And dec3 <= 100 Then
Return True
Else : Return False
End If
End If
End If
End Function
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
'clear the textboxes
Me.StudentTextBox.Text = ""
Me.IDTextBox.Text = ""
Me.Test1TextBox.Text = ""
Me.Test2TextBox.Text = ""
Me.Test3TextBox.Text = ""
Me.AverageDisplay.Text = ""
Me.GradeLabel.Text = ""
End Sub
'Validate TextBoxes and change forecolor to red if data is invalid
Private Sub Test1TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Test1TextBox.Validating, Test2TextBox.Validating, _
Test3TextBox.Validating
If mblngrade = True Then
e.Cancel = False
End If
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
End Class