Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys, I created a windows calculator by watching video in youtube... in here everything went well...
but
when we press enter key in numeric keypad in keyboard...
what ever the button selected in calculator get pressed...
For example:
If a button 8 is selected(highlighted) in calculator, and we entered some query like 6+9 and for the result if we press enter key.. instead of giving the result 15 it was giving 8.

If enter button is highlighted in calculator, and we entered 6+9 and after pressing enter it was giving result 15...
What ever the button in calculator highlighted by pressing buttons by mouse pointers the last pressed button gets highlighted and if we press enter in keyboard that button's values get inserted in the text field.. the code I used to created this calculator is....,

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace windows_calc
{
    public partial class Calculator : Form
    {
        Double value = 0;
        string operation = "";
        bool operation_pressed = false;

        public Calculator()
        {
            InitializeComponent();
        }

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

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

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



            }
        }
        private void button16_Click(object sender, EventArgs e)
        {
            equation.Text = "";
            switch(operation)
            {
                case "+":
                    result.Text = (value + Double.Parse(result.Text)).ToString();
                    break;
                case "-":
                    result.Text = (value - Double.Parse(result.Text)).ToString();
                    break;
                case "*":
                    result.Text = (value * Double.Parse(result.Text)).ToString();
                    break;
                case "/":
                    result.Text = (value / Double.Parse(result.Text)).ToString();
                    break;
                default:
                    break;
            }
            value = Int32.Parse(result.Text);
            operation = "";


        }

        private void button17_Click(object sender, EventArgs e)
        {
            result.Text = "0";
            value = 0;
            equation.Text = "";
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar.ToString())
            {
                case "0":
                    zero.PerformClick();
                    break;
                case "1":
                    one.PerformClick();
                    break;
                case "2":
                    two.PerformClick();
                    break;
                case "3":
                    three.PerformClick();
                    break;
                case "4":
                    four.PerformClick();
                    break;
                case "5":
                    five.PerformClick();
                    break;
                case "6":
                    six.PerformClick();
                    break;
                case "7":
                    seven.PerformClick();
                    break;
                case "8":
                    eight.PerformClick();
                    break;
                case "9":
                    nine.PerformClick();
                    break;
                case "-":
                    sub.PerformClick();
                    break;
                case "*":
                    times.PerformClick();
                    break;
                case "/":
                    div.PerformClick();
                    break;
                case "+":
                    add.PerformClick();
                    break;
                case "=":
                    equal.PerformClick();
                    break;


                default:
                    break;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("thanks for using Windows calulator");
        }
    }
}
Posted
Updated 4-Jun-14 20:50pm
v3
Comments
Sergey Alexandrovich Kryukov 5-Jun-14 2:58am    
The code is horrific. Start over, really. Get rid of those multiple "case" lines. Never repeat the same code. Do you know any abstractions, such as method parameters? :-)
Right now, what you are doing is opposite to programming.
—SA
chaitanya556 5-Jun-14 3:08am    
oh.. can you elaborate clearly? mr.Sergey...?
syed shanu 5-Jun-14 3:29am    
THERE IS LOTS OF SOURCE CODE AVAILABLE FOR CALCULATOR CHK THIS LINKS :

http://code.msdn.microsoft.com/windowsdesktop/Simple-Calculator-54ec8e4a

http://www.c-sharpcorner.com/UploadFile/davidkulbok/CalculatorinCSharpWindowsAppliaction11292005015717AM/CalculatorinCSharpWindowsAppliaction.aspx

http://www.sourcecodeera.com/blogs/Samath/Simple-Calculator-using-C-.aspx

http://code.msdn.microsoft.com/windowsdesktop/Simple-Calculator-d1d8cf4c
Debabrata_Das 5-Jun-14 6:01am    
Hello friend, I could not figure out what is the issue. Do you want that whenever user hits Enter key, it should produce the result irrespective of the button is selected?

navya556
oh.. can you elaborate clearly?
Sure. It refers to my comment to the question. Actually, the code is totally unacceptable. It's better not programming at all than programming this way. Not to worry, it can be learned and understood. This is one of the very basic idea in programming: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

Take this multiple "case" lines. You are doing the same thing without any reason. How about HandleDigitClick(e.KeyChar.ToString())? What this method should do? Not the "switch". Ultimately, you will operate on some numbers. And, for example, int.Parse(string) will give you the integer value corresponding to your click. But even this makes no sense, because you should operate not on digits, but on numbers.

A click should be translated into something like.
C#
MyTextBox.Text += MyTextBox.Text + e.KeyChar.ToString();

And later, when doing some mathematical operation, you would do, say, int.Parse(MyTextBox.String); or TryParse.

This way, you can get rid of all those "case" lines, all of them.

But will it be good enough? No, it could be useful as a programming exercise, but not make a useful calculator. What are you trying to achieve, a calculator with buttons? But who needs those, ever? Years ago, they were introduced as a cheap and primitive calculation device, when people simply did not have personal computers. But now? The calculator can be just a text area where you can write any expressions and then, say, click "Run". Is it too hard? For you, at this moment, it may look hard to do, but actually this is a very easy task; and almost everything is already available.

(And yes, Windows calculator is just ridiculous. People just use it by inertia and some ignorance.)

—SA
 
Share this answer
 
v2
Comments
DamithSL 5-Jun-14 3:45am    
my 5!
chaitanya556 5-Jun-14 5:13am    
thank you guys... for your valuable suggestions..
Debabrata_Das 5-Jun-14 5:56am    
Hello SA, Nice explanation!

One correction is required:
MyTextBox.Text += MyTextBox.Text + e.KeyChar.ToString();

It should be either:
MyTextBox.Text = MyTextBox.Text + e.KeyChar.ToString();
OR
MyTextBox.Text += e.KeyChar.ToString();

Please correct me if I'm wrong.

- DD
Your code is really bad and hard to maintain, you need to make some investigations and practices.
Solution 1 seems like not answering your main problem, it suggests a better design.

It is a generic problem you can read details from here
http://msdn.microsoft.com/en-us/library/aa984346(v=vs.71).aspx[^]

Quote:
Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus. (The exceptions to this are when the control with focus is another button — in that case, the button with the focus will be clicked — or a multiline text box, or a custom control that traps the ENTER key.)


Last clicked button has still focus and pressing "Enter" clicks it again, to solve it you need to clear focus on button at the end of the button click event. Just add this line at the end of your button click events, it will set focus to main form.


C#
private void button_Click(object sender, EventArgs e)
{
  //Your operations

  this.Focus();
}


And also, if you always want to use "Enter" key to calculate result, just set equal button as "Accept Button" of form;
C#
this.AcceptButton = equal;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900