Solved

Calculator help, displaying scores?

Posted on 2014-04-13
11
426 Views
Last Modified: 2014-04-14
I want to be able to display up to 10 numbers. For example, if the user enters more than 10 scores, a dialog box should appear stating that they are only allowed to enter 10 scores.  When the user clicks the Add button, the Score textbox  should clear so the user can enter another score. How do I do this? Any help would be greatly appreciated.

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;

namespace Calculator2
{


    public partial class Form1 : Form
    {
        List<int> scores = new List<int>();
        private int Total = 0;
        private int Average = 0;
        public Form1()
        {
            InitializeComponent();
            TextBox[] tbs = new TextBox[] { txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Enabled = false;
            txtScore.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            txtScore.Focus();
            btnAdd.Click += btnAdd_Click;
            btnDisplay.Click += btnDisplay_Click;
            btnClear.Click += btnClear_Click;
            btnExit.Click += btnExit_Click;
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (Char)Keys.Back && !Char.IsNumber(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == (Char)Keys.Enter)
                AddingNumbers();
        }
       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddingNumbers();
        }

        private void AddingNumbers()
        {
            int ScoreInput;
            if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out ScoreInput))
            {
                txtScore.Focus();
                MessageBox.Show("Please enter a valid score", "Input Error");
                txtScore.Focus();
                return;
            }
            else if ((ScoreInput < 0) || (ScoreInput > 100))
            {
                txtScore.Focus();
                MessageBox.Show("The score must be between 0 and 100", "Input Error");
                txtScore.Focus();
                return;
            }
            scores.Add(10);
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }
            Total = ScoreInput + Total;
            Average = Total / scores.Count;
            txtTotal.Text = Total.ToString();
            txtAverage.Text = Average.ToString(string.Empty);
            txtCount.Text = scores.Count.ToString(string.Empty);
            txtScore.Text = string.Empty;
            txtScore.Focus();
        }
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            scores.Sort();
            StringBuilder sb = new StringBuilder();
            for (int i1 = 0; i1 < scores.Count; i1++)
                sb.AppendLine(i1.ToString());
            MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            scores = null;
            TextBox[] tbs = new TextBox[] { txtScore, txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Text = String.Empty;
            txtScore.Focus();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Open in new window

0
Comment
Question by:ryanbecker24
  • 6
  • 4
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39998653
You just need to change the start of your AddingNumbers() method to something like:
private void AddingNumbers()
{
    // validate - check that we have less than 10 items
    if (scores.Count == 10)
    {
         MessageBox.Show("You can only enter 10 scores");
         return;
    }

    // clear textbox ready for input
    txtScore.Text = string.Empty;

    ...

Open in new window

0
 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 167 total points
ID: 39998657
private void AddingNumbers()
        {
            int ScoreInput;
            if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out ScoreInput))
            {
                txtScore.Focus();
                MessageBox.Show("Please enter a valid score", "Input Error");
                txtScore.Focus();
                return;
            }
            else if ((ScoreInput < 0) || (ScoreInput > 100))
            {
                txtScore.Focus();
                MessageBox.Show("The score must be between 0 and 100", "Input Error");                
                txtScore.Focus();
                return;
            }
            else if (scores.Count >= 10)
            {
                MessageBox.Show("Only 10 scores are allowed which are already given");
                txtScore.Clear();
                txtScore.Focus();
                return;
            }
           ....
        }
0
 

Author Comment

by:ryanbecker24
ID: 40000196
Thanks, do you know how to sort scores in ascending order and display them in a message box?

My display button displays shows a message box, but it doesn't show my scores, it shows the list of the amount of numbers. For example, if I typed in 4 numbers, it displays 0, 1, 2, 3.

What am I doing wrong?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 333 total points
ID: 40000318
You are already sorting the list with the line:
scores.Sort();[/b]
The reason you are getting the output that you are is because you are outputting the index for your loop, rather than the values in your list.

Try:
[code]
            scores.Sort();
            StringBuilder sb = new StringBuilder();
            for (int i1 = 0; i1 < scores.Count; i1++)
                sb.AppendLine(scores[i1].ToString());
            MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 40000392
Thanks, it doesn't display 0, 1, ... anymore.

Though, when I typed in 1, 3, 7, 9, 14, 27, 39, 54, 43, 55. I then got 0, 1, 4, 11, 20, 34, 61, 100, 154, 197.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40000421
I can't see where in the code you have posted so far, but it looks like you are somehow managing to go all Fibonacci sequence on your list of numbers.
0
 

Author Comment

by:ryanbecker24
ID: 40000425
Ok here it is.

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;

namespace Calculator2
{


    public partial class Form1 : Form
    {
        List<int> scores = new List<int>();
        private int Total = 0;
        private int Average = 0;
        public Form1()
        {
            InitializeComponent();
            TextBox[] tbs = new TextBox[] { txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Enabled = false;
            txtScore.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            txtScore.Focus();
            btnAdd.Click += btnAdd_Click;
            btnDisplay.Click += btnDisplay_Click;
            btnClear.Click += btnClear_Click;
            btnExit.Click += btnExit_Click;
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (Char)Keys.Back && !Char.IsNumber(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == (Char)Keys.Enter)
                AddingNumbers();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddingNumbers();

        }

        private void AddingNumbers()
        {
            int ScoreInput;
            if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out ScoreInput))
            {
                txtScore.Focus();
                MessageBox.Show("Please enter a valid score", "Input Error");
                txtScore.Focus();
                return;
            }
            else if ((ScoreInput < 0) || (ScoreInput > 100))
            {
                txtScore.Focus();
                MessageBox.Show("The score must be between 0 and 100", "Input Error");
                txtScore.Focus();
                return;
            }
            else if (scores.Count >= 10)
            {
                MessageBox.Show("Only 10 scores are allowed which are already given");
                txtScore.Clear();
                
                txtScore.Focus();
                return;
            }
            
          
            scores.Add(Total);
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }
            Total = ScoreInput + Total;
            Average = Total / scores.Count;
            txtTotal.Text = Total.ToString();
            txtAverage.Text = Average.ToString(string.Empty);
            txtCount.Text = scores.Count.ToString(string.Empty);
            txtScore.Text = string.Empty;
            txtScore.Focus();

            
        }
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            scores.Sort();
            StringBuilder sb = new StringBuilder();
            for (int i1 = 0; i1 < scores.Count; i1++)
                sb.AppendLine(scores[i1].ToString());
            MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
            
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            scores = null;
            TextBox[] tbs = new TextBox[] { txtScore, txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Text = String.Empty;
                
            txtScore.Focus();
          
            
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 333 total points
ID: 40000432
OK, in this line:
scores.Add(Total);

Open in new window

You are adding your running total to the list, rather than the inputted number.
It should say:
scores.Add(ScoreInput);

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 40000475
Thanks, it works. I want to create a new list after pressing the clear button. How do I do it?

I tried to and I end up having the textboxes initially cleared and then I add more numbers after and average and total don't reset.

Here's my code:

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;

namespace Calculator2
{


    public partial class Form1 : Form
    {
        List<int> scores = new List<int>();
        private int Total = 0;
        private int Average = 0;
        public Form1()
        {
            InitializeComponent();
            TextBox[] tbs = new TextBox[] { txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Enabled = false;
            txtScore.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            txtScore.Focus();
            btnAdd.Click += btnAdd_Click;
            btnDisplay.Click += btnDisplay_Click;
            btnClear.Click += btnClear_Click;
            btnExit.Click += btnExit_Click;
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (Char)Keys.Back && !Char.IsNumber(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == (Char)Keys.Enter)
                AddingNumbers();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddingNumbers();

        }

        private void AddingNumbers()
        {
            int ScoreInput;
            if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out ScoreInput))
            {
                txtScore.Focus();
                MessageBox.Show("Please enter a valid score", "Input Error");
                txtScore.Focus();
                return;
            }
            else if ((ScoreInput < 0) || (ScoreInput > 100))
            {
                txtScore.Focus();
                MessageBox.Show("The score must be between 0 and 100", "Input Error");
                txtScore.Focus();
                return;
            }
            else if (scores.Count >= 10)
            {
                MessageBox.Show("Only 10 scores are allowed which are already given");
                txtScore.Clear();
                
                txtScore.Focus();
                
                return;
            }
            
          
            scores.Add(ScoreInput);
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }
            Total = ScoreInput + Total;
            Average = Total / scores.Count;
            txtTotal.Text = Total.ToString();
            txtAverage.Text = Average.ToString(string.Empty);
            txtCount.Text = scores.Count.ToString(string.Empty);
            txtScore.Text = string.Empty;
            txtScore.Focus();

            
        }
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            scores.Sort();
            StringBuilder sb = new StringBuilder();
            for (int i1 = 0; i1 < scores.Count; i1++)
                sb.AppendLine(scores[i1].ToString());
            MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
            
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            scores = null;
            TextBox[] tbs = new TextBox[] { txtScore, txtTotal, txtCount, txtAverage };
            foreach (TextBox tb in tbs)
                tb.Text = String.Empty;
                
            txtScore.Focus();
           
            scores = new List<int>();
            txtScore.Clear();
            txtCount.Clear();
            txtAverage.Clear();
            txtTotal.Clear();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 40000499
Nevermind
0
 

Author Closing Comment

by:ryanbecker24
ID: 40000503
Thanks, all of you.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question