Question
Must be in Visual Basic code Extra 8-1 Display a test scores array In this exercise, youll enhance the Score Calculator form of extra exercise
Must be in Visual Basic code
Extra 8-1 Display a test scores array
In this exercise, youll enhance the Score Calculator form of extra exercise 4-2 so it saves the scores the user enters in an array and then lets the user display the sorted scores in a dialog box
Open the ScoreCalculator project in the Extra Exercises\Chapter 08\ScoreCalculator With Array directory. This is the Score Calculator form from extra exercise 4-2 with data validation and exception handling added.
Declare a module-level variable for an array that can hold up to 20 scores.
Modify the Click event handler for the Add button so it adds the score thats entered by the user to the next element in the array. To do that, you can use the score count variable to refer to the element.
Move the Clear Scores button as shown above. Then, modify the Click event handler for this button so it removes any scores that have been added to the array. The easiest way to do that is to create a new array and assign it to the array variable.
Add a Display Scores button that sorts the scores in the array, displays the scores in a dialog box, and moves the focus to the Score text box. Be sure that only the elements that contain scores are displayed.
Test the application to be sure it works correctly.
This is what I have to start with:
Public Class Form1
Dim total As Integer = 0
Dim count As Integer = 0
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
If IsValidData() Then
Dim score As Integer = CInt(txtScore.Text)
total += score
count += 1
Dim average As Integer = CInt(Math.Round(total / count, 0, MidpointRounding.AwayFromZero))
txtScoreTotal.Text = total.ToString
txtScoreCount.Text = count.ToString
txtAverage.Text = average.ToString
txtScore.Select()
End If
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf &
ex.GetType().ToString & vbCrLf &
ex.StackTrace, "Exception")
End Try
End Sub
Public Function IsValidData() As Boolean
Return _
IsPresent(txtScore, "Score") AndAlso
IsInt32(txtScore, "Score") AndAlso
IsWithinRange(txtScore, "Score", 1, 100)
End Function
Public Function IsPresent(textbox As TextBox, name As String) _
As Boolean
If textbox.Text = "" Then
MessageBox.Show(name & " is a required field.", "Entry Error")
textbox.Select()
Return False
Else
Return True
End If
End Function
Public Function IsInt32(textbox As TextBox, name As String) _
As Boolean
Dim number As Integer = 0
If Int32.TryParse(textBox.Text, number) Then
Return True
Else
MessageBox.Show(name & " must be an integer.", "Entry Error")
textbox.Select()
textbox.SelectAll()
Return False
End If
End Function
Public Function IsWithinRange(textbox As TextBox, name As String,
min As Decimal, max As Decimal) As Boolean
Dim number As Decimal = CDec(textbox.Text)
If number < min OrElse number > max Then
MessageBox.Show(name & " must be between " & min & " and " &
max & ".", "Entry Error")
textbox.Select()
textbox.SelectAll()
Return False
Else
Return True
End If
End Function
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
total = 0
count = 0
txtScore.Text = ""
txtScoreTotal.Text = ""
txtScoreCount.Text = ""
txtAverage.Text = ""
txtScore.Select()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started