Solved

Calculator help, buttons not working?

Posted on 2014-04-13
6
146 Views
Last Modified: 2014-04-13
My buttons aren't working

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();
        }
        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(20);
            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 buttonExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}

Open in new window

0
Comment
Question by:ryanbecker24
  • 4
  • 2
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39997884
I see where you attached a handler for key presses here:

txtScore.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

Open in new window


Did you do the same for the buttons?
0
 

Author Comment

by:ryanbecker24
ID: 39997898
How do I do that?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39997929
btnAdd.Click += btnAdd_Click;
btnDisplay.Click += btnDisplay_Click;
btnClear.Click += btnClear_Click;

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ryanbecker24
ID: 39997935
I want to enter or click for the add button, but only click the others.
0
 

Author Comment

by:ryanbecker24
ID: 39997943
Thank you the buttons work now. My only problems now is that I want to display up to 10 numbers and also it's not displaying the numbers correctly. Anyway you could help me out with that?
0
 

Author Comment

by:ryanbecker24
ID: 39997950
Thank you the buttons work now. My only problems now is that I want to display up to 10 numbers and also it's not displaying the numbers correctly. Anyway you could help me out with that?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now