Visual Basic 2005 Guide
Average Exam Marks Program
Introduction
This page includes full instructions for making the exam marks program. Making this program will show you,
- Local & global variables
- Responding to the user clicking a button
- Working out what the user has entered into a text box
- Adding items to a list box
- Changing the text in a text box
- Placing the cursor in the text box of your choice
You may wish to revisit this page and the program you write next time you need to do any of the above.
Programming Objectives
Our program will allow the user to type in an exam marks, pressing a button after each one. Each time the button is pressed, the mark is added to the list box, the total of the marks entered is displayed as well as the number of marks and the mean average of those marks.
Step 1 - Form Design
Start a new project called Mean Marks, save the project in its own folder. Your form is going to look like this (the numbers are included as a key - you won't be adding them).
Number | Control | Name | Text Property |
---|---|---|---|
1 | Label | lblMark | Enter Mark |
2 | TextBox | txtMark | |
3 | Button | btnAdd | Add Mark |
4 | ListBox | lstMarks | |
5 | Label | lblTotal | Total marks |
6 | Label | lblNumber | Number Of Marks |
7 | Label | lblMean | Mean Mark |
8 | TextBox | txtTotal | |
9 | TextBox | txtNumber | |
10 | TextBox | txtMean |
Use the information in the table to create the form as it is shown on above.
Step 2 - Declaring Global Variables
Our program will need to keep track of two pieces of information in order to calculate the mean of the marks. The mean of the marks is the total of the marks divided by the number of marks entered.
Click on View, Code on the menu and enter the following lines immediately below the line, Public Class frmMean.
Dim sglTotal As Single
Dim intNumMarks As Integer
The total mark is declared as a single to allow the user to enter scores with decimals (half marks might be possible). The number of marks is an integer since this must always be a whole number.
Step 3 - Writing The Button Click Handler
Double click on the button on the form to bring up the code for the button's click event. Enter the following code and save your work.
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim sglMarkEntered As Single, sglMean As Single
sglMarkEntered = txtMark.Text
sglTotal = sglTotal + sglMarkEntered
intNumMarks = intNumMarks + 1
sglMean = sglTotal / intNumMarks
lstMarks.Items.Add(sglMarkEntered)
txtTotal.Text = sglTotal
txtNumber.Text = intNumMarks
txtMean.Text = sglMean
txtMark.Text = ""
txtMark.Focus()
End Sub
Run the program to test that it works. Correct any obvious errors and then read the next section carefully.
Step 4 - Understanding The Code
It is important for you to be able to understand what each of the lines of code do here.
Dim sglMarkEntered As Single, sglMean As Single | Declares 2 variables to store the mark that has been entered and the mean that will be calculated. |
sglMarkEntered = txtMark.Text | Makes sglMarkEntered equal to the mark that the user has typed into the txtMark text box. |
sglTotal = sglTotal + sglMarkEntered | Adds the new mark onto the total. Notice how we do this. |
intNumMarks = intNumMarks + 1 | Adds 1 to the number of marks. |
sglMean = sglTotal / intNumMarks | Sets sglMean to equal the total mark divided by the number of marks. |
lstMarks.Items.Add(sglMarkEntered) | Adds the new mark to the list box |
txtTotal.Text = sglTotal | Sets the text in the txtTotal text box to equal the total of marks entered. |
txtNumber.Text = intNumMarks | Sets the text in the txtNumber text box to equal the number of marks entered. |
txtMean.Text = sglMean | Sets the text in the txtMean text box to equal the mean exam mark. |
txtMark.Text = "" | Empties the txtMark text box by setting its text to equal nothing. |
txtMark.Focus() | Places the cursor in the txtMark text box ready for the user to enter another mark. |