Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calculator help, displaying scores?

Posted on 2014-04-13
11
Medium Priority
?
454 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 668 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1332 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 1332 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

876 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