In: Computer Science
This Module is mainly to practice If ... Then ... ElseIf ... with creating a project in Visual Basics
If Average < 60 Then
lblAvg.Text = Average
lblGrade.Text = "F"
ElseIf Average < 70 Then
lblAvg.Text = Average
lblGrade.Text = "D"
ElseIf Average < 80 Then
lblAvg.Text = Average
lblGrade.Text = "C"
ElseIf Average < 90 Then
lblAvg.Text = Average
lblGrade.Text = "B"
ElseIf Average < 100 Then
lblAvg.Text = Average
lblGrade.Text = "A"
End If
txtTest1.Text = ""
lblAvg.Text = ""
lblGrade.Text = ""
Form layout with controls
User Interface frmMain.vb[Design]:
Screen showing controls :
************************************
'This is the VB from
Public Class frmMain
'This is click event for click button
Private Sub btnClear_Click(sender As Object, e As EventArgs)
Handles btnClear.Click
'When clear button is clicked clear the textbox and labels
lblAverage.Text = "" 'clear average label
lblGrade.Text = "" 'Clear grade label
txtTest1.Text = "" 'Clear Test1 textbox
txtTest2.Text = "" 'Clear Test2 textbox
txtTest3.Text = "" 'Clear Test3 textbox
End Sub
'This is click event for calculate average button
Private Sub btnCalcAvg_Click(sender As Object, e As EventArgs)
Handles btnCalcAvg.Click
Dim intTest1 As Integer 'This variable will store score for
Test1
Dim intTest2 As Integer 'This variable will store score for
Test2
Dim intTest3 As Integer 'This variable will store score for
Test3
Dim intAverage As Integer 'This variable will store average for
test scores
intTest1 = Integer.Parse(txtTest1.Text) 'Take score for Test1 and
convert it into integer
intTest2 = Integer.Parse(txtTest2.Text) 'Take score for Test2 and
convert it into integer
intTest3 = Integer.Parse(txtTest3.Text) 'Take score for Test3 and
convert it into integer
'This line will calculate average and store this average in the
variable intAverage
intAverage = (intTest1 + intTest2 + intTest3) / 3
'Using if else statement in VB to check average of the test
scores
If intAverage < 60 Then
'when average of test scores is less than 60 then
lblAverage.Text = intAverage 'print intAverage
lblGrade.Text = "F" 'print grade for avergae less than 60 which is
F
ElseIf intAverage < 70 Then
'when average of test scores is less than 70 then
lblAverage.Text = intAverage 'print intAverage
lblGrade.Text = "D" 'print grade for avergae less than 70 which is
D
ElseIf intAverage < 80 Then
'when average of test scores is less than 80 then
lblAverage.Text = intAverage 'print intAverage
lblGrade.Text = "C" 'print grade for avergae less than 80 which is
C
ElseIf intAverage < 90 Then
'when average of test scores is less than 90 then
lblAverage.Text = intAverage 'print intAverage
lblGrade.Text = "B" 'print grade for avergae less than 90 which is
B
ElseIf intAverage < 100 Then
'when average of test scores is less than 100 then
lblAverage.Text = intAverage 'print intAverage
lblGrade.Text = "A" 'print grade for avergae less than 100 which is
A
End If
End Sub
End Class
================================================
Screen showing average of three test scores and grade :