Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello am an intern as a junior web developer I was given a calculator application I did everything that I know in a calculator but now I have a problem because my calculator does not want to calculate all the values for example: when I type 5+5+5+5 on my textbox it gives me 15 can someone please help me with the solution?

What I have tried:

here is my code so far

C#
using my_calculator_2022.Properties;
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 my_calculator_2022
{
    public partial class Form1 : Form
    {

        double FirstNumber;
        string Operation;
        public double result = 0;
        bool checkOpPress = true;
        private Calc calc;

        public Form1()
        {
            InitializeComponent();

            txtvalue.Multiline = true;
            txtvalue.ScrollBars = ScrollBars.Both;


            txtvalue.KeyDown += txtvalue_KeyDown;
            txtvalue.KeyPress += txtvalue_KeyPress;
            txtvalue.KeyUp += txtvalue_KeyUp;

            calc = new Calc();

        }


        private void txtvalue_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void txtvalue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.A)
            {
                MessageBox.Show("A key pressed");

            }
        }

        private void txtvalue_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MessageBox.Show(" Enter key Released");

            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MessageBox.Show(" Enter key Down");
            }


        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '0')
            {
                e.Handled = true;
                equals.PerformClick();
            }



        }

        private void btn_one_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "7";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "7";
            }

        }

        private void btn_two_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "8";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "8";
            }

        }

        private void btn_three_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "9";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "9";
            }

        }

        private void btn_four_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "4";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "4";
            }

        }

        private void btn_five_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "5";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "5";
            }

        }

        private void btn_six_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "6";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "6";
            }

        }

        private void btn_seven_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "1";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "1";
            }

        }

        private void btn_eight_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "2";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "2";
            }


        }

        private void btn_nine_Click(object sender, EventArgs e)
        {
            if (txtvalue.Text == "0" && txtvalue.Text != null)
            {
                txtvalue.Text = "3";
            }
            else
            {
                txtvalue.Text = txtvalue.Text + "3";
            }

        }

        private void btn_zero_Click(object sender, EventArgs e)
        {
            txtvalue.Text = txtvalue.Text + "0";


        }

        private void btn_div_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtvalue.Text);
            txtvalue.Text = "0";
            Operation = "/";

            if (checkOpPress == false)
            {
                calc.SetRes();

            }


            checkOpPress = false;

        }

        private void btn_mult_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtvalue.Text);
            txtvalue.Text = "0";
            Operation = "*";

            if (checkOpPress == false)
            {
                calc.SetRes();

            }


            checkOpPress = false;
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtvalue.Text);
            txtvalue.Text = "0";
            Operation = "-";

            if (checkOpPress == false)
            {
                calc.SetRes();

            }


            checkOpPress = false;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
           FirstNumber = Convert.ToDouble(txtvalue.Text);
            txtvalue.Text = "0";
            Operation = "+";
            txtvalue.Text =

            if (checkOpPress == false)
            {
                calc.SetRes();

            }


            checkOpPress = false;
        }

        private void btn_dot_Click(object sender, EventArgs e)
        {
            txtvalue.Text = txtvalue.Text + ".";
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {

        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            int index = txtvalue.Text.Length;
            index--;
            txtvalue.Text = txtvalue.Text.Remove(index);
            if (txtvalue.Text == "")
            {
                txtvalue.Text = "0";

            }

        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            txtvalue.Text = "0";
        }

        private void btn_equal_Click(object sender, EventArgs e)
        {
            
            double SecondNumber;
            double ThirdNumber;
            double Result;

            SecondNumber = Convert.ToDouble(txtvalue.Text);
            ThirdNumber = Convert.ToDouble(txtvalue.Text);

            if (Operation == "+")
            {
                Result = (FirstNumber + SecondNumber) + ThirdNumber;
                txtvalue.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
            if (Operation == "-")
            {
                Result = (FirstNumber - SecondNumber);
                txtvalue.Text = Convert.ToString(Result);
               FirstNumber = Result;
            }
            if (Operation == "*")
            {
                Result = (FirstNumber * SecondNumber);
                txtvalue.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
            if (Operation == "/")
            {
                if (SecondNumber == 0)
                {
                    txtvalue.Text = "Cannot divide by zero";

                }
                else
                {
                    Result = (FirstNumber / SecondNumber);
                   txtvalue.Text = Convert.ToString(Result);
                    FirstNumber = Result;
                }

            }
        }

        private void btn_add_KeyPress(object sender, KeyPressEventArgs e)
        {
            MessageBox.Show ("Test");
        }


        private void txtvalue_Enter(object sender, EventArgs e)
        {
            btn_add.Focus();
            btn_mult.Focus();
            btn_sub.Focus();
            btn_div.Focus();
            btn_cancel.Focus();
            btn_clear.Focus();
            btn_dot.Focus();
            btn_back.Focus();
            btn_one.Focus();
            btn_two.Focus();
            btn_three.Focus();
            btn_four.Focus();
            btn_five.Focus();
            btn_six.Focus(); 
            btn_seven.Focus();
            btn_eight.Focus();
            btn_nine.Focus();
            btn_zero.Focus();

        }
    }
Posted
Updated 19-Jan-22 3:16am
v2
Comments
[no name] 19-Jan-22 2:13am    
You should study working examples as part of your learning process; helps get your bearings. This is a calculator walk-through.

Windows Forms Designer tutorial - Visual Studio (Windows) | Microsoft Docs[^]
BillWoodruff 19-Jan-22 20:12pm    
This idea and its code is so confused i suggest you get a good back on C# and study the "basics," and, re-think your design. What type of calculator increments some variable by #7 each time a mouse-click on a button occurs ?

There are lots of examples of C# calculators on-line: study them !

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

And the code you have ... oh dear. The names of the controls are all wrong: "btn_three" generates the number "9", "btn_two" is "8", and so on ... Why do you have separate handlers for each number button? Why not use a single handler?
To be honest, it looks like it was thrown together without much thought at all ...
 
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