0

I made this calculator in C# and I have one problem: When I press keyboard something like 1 it gives me incorrect result double number. I don't get the correct result.

Do you know what I am doing wrong,

Thank you very much!

namespace Calculator
{
    public partial class Calculator : Form
    {
        Double value = 0;
        String operation = "";
        int i = 0;
        bool operation_pressed = false;
        bool button_dot = false;
        OperationClass class1 = new OperationClass();
        public Calculator()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(Calculator_KeyPress);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Calculator_Load(object sender, EventArgs e)
        {

        }

        private void Calculator_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((result.Text == "0") || (operation_pressed))
                result.Clear();
            switch (e.KeyChar)
            {
                // key press from 0-9
                case (char)48:
                case (char)49:
                case (char)50:
                case (char)51:
                case (char)52:
                case (char)53:
                case (char)54:
                case (char)55:
                case (char)56:
                case (char)57:
                    e.Handled = true;
                    result.Text += e.KeyChar.ToString();
                    break;
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            if ((result.Text == "0") || (operation_pressed))
                result.Clear();
            operation_pressed = false;
            Button b = (Button)sender;
            result.Text = result.Text + b.Text;
        }

        private void operator_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text;
            value = Double.Parse(result.Text);
            operation_pressed = true;
            equation.Text = value + " " + operation;
        }

        private void buttonCE_Click(object sender, EventArgs e)
        {
            result.Text = "0";
            equation.Text = "";
            button_dot = false;
        }

        private void buttonC_Click(object sender, EventArgs e)
        {
            result.Clear();
            value = 0;
        }

        private void buttonEqual_Click(object sender, EventArgs e)
        {
            equation.Text = "";
            button_dot = false;
            operation_pressed = true;
            if (operation != "")
                result.Text = class1.GetResult(operation, value, result.Text);
            else
                result.Text = result.Text + "";
        }

        private void buttonDot_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if ((!button_dot && !operation_pressed) || result.Text == "0")
                result.Text = result.Text + b.Text;
            button_dot = true;
        }
    }
}
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • Have you got an event handler declaring in the designer as well. Maybe the Calculator.designer.cs is also declaring a KeyPress event hander. – Robert Slaney Mar 04 '14 at 04:45
  • Example: When I press 1 on keyboard, the result is 11 . I debug by VS 2010 and function Calculator_Keypress ran twice. –  Mar 04 '14 at 05:34
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 04 '14 at 07:04

2 Answers2

0

I'd change it from keypress to KeyUp instead. That way it only fires as soon as the key is released.

Edit: Check out this info: Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio

Community
  • 1
  • 1
RAhnemann
  • 31
  • 3
  • Thanks for your support! I was add `if (!e.Handled)` before `switch`code. –  Mar 04 '14 at 04:50
0

You don't need to append keychar in your result.Text (Assuming result is your TextBox item) as you have written in line result.Text += e.KeyChar.ToString();. That's the line which is doubling your input.

Remove it and it'll work as expected.

Rumit Parakhiya
  • 2,674
  • 3
  • 24
  • 33