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
Solved

Calculator help, displaying scores?

Posted on 2014-04-13
11
433 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

789 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