WPF C# Tutorial – Making a Calculator App in Visual Studio

Full Code – Use as a reference to the tutorial. Double check the full code here to make sure you followed along correctly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace calculations
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        double num1; // this is double data type number called num1
        double num2; // this is double data type number called num2
        double totalNumber; // this is the double data number called totalNumber this will be used to show the total calculation

        public MainWindow()
        {
            InitializeComponent();

        }

        private void AddingNumbers(object sender, RoutedEventArgs e)
        {
            num1 = Convert.ToDouble(numberA.Text); // catch the text from the number A text box and convert to a double data type
            num2 = Convert.ToDouble(numberB.Text); // catch the text from the  number B text box and convert to a double data type

            totalNumber = num1 + num2; // add the two numbers together and place them in the total number variable

            result.Content = totalNumber.ToString(); // show the total number on the result label
        }

        private void SubtractNumbers(object sender, RoutedEventArgs e)
        {
            num1 = Convert.ToDouble(numberA.Text); // catch the text from the number A text box and convert to a double data type
            num2 = Convert.ToDouble(numberB.Text); // catch the text from the  number B text box and convert to a double data type

            totalNumber = num1 - num2; // substract the two numbers together and place them in the total number variable

            result.Content = totalNumber.ToString(); // show the total number on the result label
        }

        private void MultiplyNumbers(object sender, RoutedEventArgs e)
        {
            num1 = Convert.ToDouble(numberA.Text); // catch the text from the number A text box and convert to a double data type
            num2 = Convert.ToDouble(numberB.Text); // catch the text from the  number B text box and convert to a double data type

            totalNumber = num1 * num2; // multiply the two numbers together and place them in the total number variable

            result.Content = totalNumber.ToString(); // show the total number on the result label
        }

        private void DivideNumbers(object sender, RoutedEventArgs e)
        {
            num1 = Convert.ToDouble(numberA.Text); // catch the text from the number A text box and convert to a double data type
            num2 = Convert.ToDouble(numberB.Text); // catch the text from the  number B text box and convert to a double data type

            totalNumber = num1 / num2; // divide the two numbers together and place them in the total number variable

            totalNumber = Math.Round(totalNumber, 2); // round the numbers down 2 decimal points

            result.Content = totalNumber.ToString(); // show the total number on the result label
        }
    }
}

End of Tutorial – Moo Out




2 responses to “WPF C# Tutorial – Making a Calculator App in Visual Studio”

  1. Dima says:

    How I need to change (NumberA.Text), (NumberB.Text) and whereat?

  2. Anhar Ali says:

    Hi, Thank you for bringing to my attention, there was a small spelling error which led to the numberA and numberB texboxes not being recognised. I have fixed that now in the source code. Both of them are the text boxes you add to the xaml file and then we call them to capture the input from them to do the calculations.