Search code examples
c#calculatorcalc

simple calculator that shows the different between 2 numbers in C#


first of all sorry for my broken language. i would like to create a calculator that shows the different between 2 numbers in C# i tried to make it but i always end up with adding the textbox 1 to the textbox 2 and displaying the total in text box 3, which i don't want to do please see the GUI , it's simple i also would like to make the number in green color if the different is positive and red if the different is negative. see the picture thanks! simple gui


Solution

  • Write this code in the click event of calculate button

        private void Calculate_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                int diff = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text));
                textBox3.Text = diff.ToString();
                if (diff >= 0)
                {
                    textBox3.ForeColor = Color.Green;
                }
                else
                    textBox3.ForeColor = Color.Red;
    
            }
            else
            {
                MessageBox.Show("PLZ Enter the balances");
            }
        }
    

    Result