Solved

Calculator help, displaying scores?

Posted on 2014-04-13
11
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

632 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