Create a form that accepts scores from the user, displays the total, count, and average of the scores, and displays a dialog box that lists the scores.This application should check the number entered by the user to make sure it is a valid integer from 0 to 100. If a value is entered outside this range, the score does not count (and should not be included in the score total, score count, or average).
Here's What I have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Scores
{
public partial class Form1 : Form
{
private List<int> Scores = new List<int>();
private int Total = 0;
private float Average = 0F;
public Form1()
{
InitializeComponent();
}
private float CalculateAverage(int sum, int n)
{
// Calculate the average of a sum of numbers and the amount n of these numbers.
return (float)sum / (float)n;
}
private void addBtn_Click(object sender, EventArgs e)
{
// Leave if there is nothing typed in the score textbox
if (scoreTxb.Text == string.Empty)
{
scoreTxb.Focus(); //this button has focus, reset to textbox
return;
}
// Get input
int Score = int.Parse(scoreTxb.Text);
// Update list
Scores.Add(Score);
// Update our Total and show in total textbox
Total += Score;
totalTxb.Text = Total.ToString();
// Update count textbox
countTxb.Text = Scores.Count.ToString();
// Update average textbox
Average = CalculateAverage(Total, Scores.Count);
averageTxb.Text = Average.ToString("0.0");
// Make score textbox ready for new input
scoreTxb.Text = string.Empty;
scoreTxb.Focus();
}
private void displayBtn_Click(object sender, EventArgs e)
{
// Sort our list
Scores.Sort();
// Construct display string
string DisplayString = "List of all scores entered :\n\n";
foreach (int i in Scores)
{
DisplayString += i.ToString() + "\n";
}
// Show the string in a MessageBox
MessageBox.Show(DisplayString);
}
private void clearBtn_Click(object sender, EventArgs e)
{
// Reset all our variables
Scores.Clear(); // empty the list
Total = 0;
Average = 0F;
// Update textboxes
totalTxb.Text = Total.ToString();
countTxb.Text = Scores.Count.ToString();
averageTxb.Text = Average.ToString("0.0");
}
//regex expression to test if your input is between 0 and 100
private const string pattern = @"^((100)|(\d{0,2}))$";
public bool IsValid(string pattern, string text, RegexOptions options)
{
return System.Text.RegularExpressions.Regex.IsMatch(text, pattern, options);
}
private void scoreTxb_TextChanged(object sender, EventArgs e)
{
TextBox TB = sender as TextBox;
if (!IsValid(pattern, TB.Text, RegexOptions.Multiline | RegexOptions.Compiled))
{
MessageBox.Show("Your input is not correct");
}
}
//close the form
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
If the number is not in the range, I don't want it included in the score total, score count, or average. Please help!
Any help is appreciated.