Link to home
Start Free TrialLog in
Avatar of ryanbecker24
ryanbecker24

asked on

Calculator help, buttons not working?

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
Avatar of ryanbecker24
ryanbecker24

ASKER

How do I do that?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I want to enter or click for the add button, but only click the others.
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?
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?