简体   繁体   中英

Simple Calculator (C#) - How do I get result of calculation involving two integers in textBox2?

I'm still fairly new to C# programming language. I have to create a simple calculator which can add, subtract, multiply, divide two numbers of users choice without decimals. My Calculator has two input text boxes and one output text box. I've only got 4 operator buttons which are; +,-,*,/. In addition, I also added a 'Backspace' button.

When user inputs the two values, they have to press one of the operator buttons. For example; if the user types in 10 in 'textBox1' and 20 in 'textBox2' and presses 'Button15', the value of the sum will be displayed in 'textBox2'.

There is link of image below the code.

So my problem is with the code under "=" Button15 - I am able to display my user input values but there is problem with my result. I have used my own approach and it might be incorrect.

I would appreciate any help.

Here's the source code:

public partial class Form1 : Form
{
    int FirstNumber;
    string Operation;
    string l;
    public Form1()
    {
        InitializeComponent();
    }


    private void button4_Click(object sender, EventArgs e)
    {
        if(textBox1.Text=="0")
        {
            textBox1.Text = "6";
        }
        else
        {
            textBox1.Text = textBox1.Text + "6";
        }
    }

    private void button15_Click(object sender, EventArgs e)
    {

        int SecondNumber;
        int Result;

        if (Operation == "+")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("+");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber + SecondNumber);
            textBox2.Text = Convert.ToString(Result);


        }
        else if (Operation == "-")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("-");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber - SecondNumber);
            textBox2.Text = Result.ToString(); 

        }
        else if (Operation == "x")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("x");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber * SecondNumber);
            textBox2.Text = Convert.ToString(Result);

        }
        else if(Operation=="/")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("/");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            if ( SecondNumber == 0)
            {
                textBox1.Text = "Cannot divide by 0";
            }
            else
            {
                Result = (FirstNumber / SecondNumber);
                textBox2.Text = Convert.ToString(Result);
            }
        }



    }

    private void button1_Click(object sender, EventArgs e)      
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "1";
        }
        else
        {
            textBox1.Text = textBox1.Text + "1";
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "2";
        }
        else
        {
            textBox1.Text = textBox1.Text + "2";
        }

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "3";
        }
        else
        {
            textBox1.Text = textBox1.Text + "3";
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "5";
        }
        else
        {
            textBox1.Text = textBox1.Text + "5";
        }
    }

    private void button6_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "4";
        }
        else
        {
            textBox1.Text = textBox1.Text + "4";
        }
    }

    private void button9_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "7";
        }
        else
        {
            textBox1.Text = textBox1.Text + "7";
        }
    }

    private void button8_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "8";
        }
        else
        {
            textBox1.Text = textBox1.Text + "8";
        }
    }

    private void button7_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "9";
        }
        else
        {
            textBox1.Text = textBox1.Text + "9";
        }
    }

    private void button10_Click(object sender, EventArgs e)
    {

            textBox1.Text = textBox1.Text + "0";

    }

    private void button11_Click(object sender, EventArgs e)
    {

       int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "-";

        textBox1.Text = textBox1.Text + Operation;


    }

    private void button12_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "+";
        textBox1.Text = textBox1.Text + Operation;


    }

    private void button13_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "x";
        textBox1.Text = textBox1.Text + Operation;


    }

    private void button14_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "/";
        textBox1.Text = textBox1.Text + Operation;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        textBox1.Text = textBox1.Text + "0";
    }


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

Here is the image of my calculator. The textBox1, the one at the top is happily displaying the user input but the one below, textBox2 is not working when clicked on "="

In your button event handlers you declare new FirstNumber and Operation variables and assign them. These aren't the same as the Form's FirstNumber and Operation . You don't need to declare them again, you can just assign to them.

private void button11_Click(object sender, EventArgs e)
{

   int FirstNumber = Convert.ToInt32(textBox1.Text);
    string Operation = "-";

    textBox1.Text = textBox1.Text + Operation;


}

should be

private void button11_Click(object sender, EventArgs e)
{

    FirstNumber = Convert.ToInt32(textBox1.Text);
    Operation = "-";

    textBox1.Text = textBox1.Text + Operation;
} 

and similar for the rest of your button clicked event handlers.

First of all, please give dhe controls and event handlers meaningful names. You can change the name in the properties columns in Design View.

Secondly, you declare new variables in the methods for handling operator button clicks. For example, you have int FirstNumber and string Operation in the event handlers. After the declaration the class variables with the same name are shadowed, and you access only the new variables inside those handlers.

Remove the int and string . The way you have it, you never change the class fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM