Question
In this exercise, youll enhance the Score Calculator form of Chapter 4-2 Lab Assignment so it saves the scores the user enters in an array
In this exercise, youll enhance the Score Calculator form of Chapter 4-2 Lab Assignment so it saves the scores the user enters in an array and then lets the user display the sorted scores in a dialog box.
This is the Score Calculator form from Chapter 4-2 Lab with data validation and exception handling added.
1. Make sure that your program contains the IsValidData(), IsPresent(), IsInt32(), and IsWithinRange() validation methods we wrote in Chapter 7.
2. Declare a class variable for an array that can hold up to 20 scores.
3. 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. Hint: you can use the variable that contains a count of the number of scores to refer to the element.
4. Move the Clear Scores button as shown in the attached form. Then, modify the Click event handler for this button so it removes any scores that have been added to the array. Hint: The easiest way to clear the array is to create a new array and assign it to the array variable (example on page 235 (Murach C# Chapter 15)- code that reuses an array variable).
5. 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
This is the code to be modified:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace ScoreCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
int total = 0; int count = 0;
private void btnExit_Click(object sender, System.EventArgs e) { this.Close(); }
private void btnAdd_Click(object sender, System.EventArgs e) { try { if (IsValidData()) { int score = Convert.ToInt32(txtScore.Text); total += score; count += 1; int average = total / count; txtScoreTotal.Text = total.ToString(); txtScoreCount.Text = count.ToString(); txtAverage.Text = average.ToString(); txtScore.Focus(); } } catch (Exception ex) { MessageBox.Show(ex.Message + " " + ex.GetType().ToString() + " " + ex.StackTrace, "Exception"); } }
private void btnClear_Click(object sender, EventArgs e) { total = 0; count = 0; txtScore.Text = ""; txtScoreTotal.Text = ""; txtScoreCount.Text = ""; txtAverage.Text = ""; txtScore.Focus();
}
public bool IsValidData() { return // Validate the Score text box IsPresent(txtScore, "Score") && IsInt32(txtScore, "Score") && IsWithinRange(txtScore, "Score", 01, 100); }
public bool IsPresent(TextBox textBox, string name) { if (textBox.Text == "") { MessageBox.Show(name + " is a required field.", "Entry Error"); textBox.Focus(); return false; } return true; }
public bool IsInt32(TextBox textBox, string name) { int number = 0; if (Int32.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(name + " must be a valid integer.", "Entry Error"); textBox.Focus(); return false; } }
public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number max) { MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error"); textBox.Focus(); return false; } return true; }
} }
Score Calculator Score: 98 Add Score total:472 Score count: 5 Average: 94 Display Scores Clear Scores Exit
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