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.

In the addBtn_Click, where you are checking that a value has been entered, you can also check the value is between 0 -100.

int value = Int.Parse(scoreTxb.Text);
if(value < 0 || 100 < value) {
   // error
}

If you are worried about potentially bad input Int.TryParse might be a good idea too.

You haven't said where the problem is. Assuming it's the regex, you might find it simpler to just validate the string as a number:

public bool IsValid(string text)
{
    int temp;
    return int.TryParse(text, out temp) && temp >=0 && temp <= 100;
}

Thanks guys but these codes did not work. There is no syntax error in my code at all I need to get the code to not include any number greater than 100. When I say not include I mean do not count it in the average, the score total, or the score count. Here's how my form looks.

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).

I'm not sure how getting the integer value at the start of the addBtn_Click method and checking if it is between 0 and 100 could fail.
Could you post up the code section where you make the check (by which method mentioned above)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.