This Module is mainly to practice If ... Then ... ElseIf ...with creating a project in Visual Basics
- Open Visual Basic and create a new Project. Select Windows FormNew Project window. You will save this project in your CS 3 folderand name it as:yourlastname-firstname-Grader.
- In this project user inputs three test scores, a buttoncalculates the average and displays the average and grade. Anotherbutton clears the entries.
- Create a form with seven labels,three textboxes and two buttons,similar to below form layout with each control.
- Here are the properties of the form and each control:
- Form: change the title of the form to: - Grader
- Label1, Text: Test1; Name: leave as is, you don’t use labels inthe program
- Label2, Text: Test2; Name: leave as is
- Label3, Text: Test3; Name: leave as is
- Label4, Text: Average; Name: leave as is
- Label5, Text: Grade; Name: leave as is
- Label6, Text: blank; Name: lblAverage ; BorderStyle: Fixed 3D;AutoSize: False
- Label7, Text: blank; Name: lblGrade ; BorderStyle: Fixed 3D;AutoSize: False
- Textbox1, Test: blank; Name: txtTest1
- Textbox2, Test: blank; Name: txtTest2
- Textbox3, Test: blank; Name: txtTest3
- Button1, Text: Calculate Average; Name: btnCalcAvg
- Button2, Text: Clear; Name: btnClear
- Write a Visual Basic program that: 1) accepts three scores, 2)computes the average and assigns the grade.
- You declare four variables as Integer data type. They are:Test1, Test2, Test3, and Average.
- You create an IF…ELSEIF… structure to assign grades using thislogic. Note: No need to create a loop:
- To assign Letter Grade use this criteria: Average<60, F; Average <70, D; Average < 80, C; Average < 90,B; Average <=100, A.
- To do this you write the code in Calculate Average buttonprocedure. First declare the variables that will get test scores(intTest1, intTest2, intTest3) from textboxes and another one(intAverage) for calculating the tests average. Then assign textboxentries into the first three variables. Then calculate the Averageand finally based on the Average assign a Letter Grade.
- Clear button clears the entries in Test1, Test2, Test3textboxes and in Average and Grade labels. Then program is ready totake another set of test scores.Ready to enter another set ofnumbers.
- After creating the following form layout in Visual Basics (seeassignment 8 on how to create a new project). Make sure to name thecontrols according to above steps 11 - 17.
- Double-click on Calculate Average, you will placed in Codewindow to write code:
- Declare 4 variables: intTest1, intTest2, intTest3, intAverageas Integer.
- Assign textbox entries for three scores into intTest1,intTest2, intTest3 variables, Example: intTest1 =txtTest1.Text
- Calculate Average, develop an expression to sum three testsvalues and divide by 3. Store Average into intAveragevariable.
- Then develop nested IF … THEN … ELSEIF … ELSEIF selectionstructure to evaluate Average and assign a Letter Grade.
- You display the average and assigned grade in Average and Gradelabel controls
- We have reviewed similar nested selection structure inSelection slides.
- You may want to start with a similar code below and make sureto make the necessary changes. Before starting to build IFstatement, don’t forget to declare the variables and assign Test1,Test2, Test3 to their corresponding variables. Also calculating theAverage of three tests:
    If Average < 60Then
           lblAvg.Text = Average
           lblGrade.Text = \"F\"
        ElseIfAverage < 70 Then
           lblAvg.Text = Average
           lblGrade.Text = \"D\"
        ElseIfAverage < 80 Then
           lblAvg.Text = Average
           lblGrade.Text = \"C\"
        ElseIfAverage < 90 Then
           lblAvg.Text = Average
           lblGrade.Text = \"B\"
        ElseIfAverage < 100 Then
           lblAvg.Text = Average
           lblGrade.Text = \"A\"
        EndIf
- To program Clear button, double-click on Clear button on theform.
- Type the code to clear the textboxes and label controlsentries:
- You can use the following sample code:
txtTest1.Text = \"\"
  lblAvg.Text = \"\"
lblGrade.Text = \"\"
                        Â
Form layout with controls