Making a Calculator in Windows Store Apps

Introduction

Today we are creating a simple calculator Windows Store App using  C# and XAML in Visual Studio 2012. This calculator app will simply perform addition, subtraction, multiplication, division and percentage calculation like the Windows Calculator but not like the scientific calculator.

Step 1

The first step is to create a new Windows Store App.

New-Windows-Store-Apps.jpg

Step 2

In the second step double-click on "MainPage.xaml":

Solution-Explorer-Windows-Store-Apps.jpg

Step 3

The "MainPage.xaml" is as in the follwoing code:

<Page

    x:Class="App2.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:App2"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="436,85,0,0" Width="380" x:Name="TextBox" FontWeight="Bold"/>

        <TextBox HorizontalAlignment="Left" x:Name="ValuText" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="436,117,0,0" Width="380"/>

        <Button Content="Plus" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="436,176,0,0" Width="80" x:Name="BtnPlus" Click="BtnPlus_Click"/>

        <Button Content="Subs" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="536,176,0,0" Width="80" x:Name="BtnMinus" Click="BtnMinus_Click"/>

        <Button Content="Multi" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="636,176,0,0" Width="80" x:Name="BtnMulti" Click="BtnMulti_Click"/>

        <Button Content="Dive" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="736,176,0,0" Width="80" x:Name="BtnDiv" Click="BtnDiv_Click"/>

        <Button Content="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="436,231,0,0" Width="80" x:Name="Btn1" Click="Btn1_Click"/>

        <Button Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="536,231,0,0" Width="80" x:Name="Btn2" Click="Btn2_Click"/>

        <Button Content="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="636,231,0,0" Width="80" x:Name="Btn3" Click="Btn3_Click"/>

        <Button Content="4" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="436,287,0,0" Width="80" x:Name="Btn4" Click="Btn4_Click"/>

        <Button Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="536,287,0,0" Width="80" x:Name="Btn5" Click="Btn5_Click"/>

        <Button Content="6" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="636,287,0,0" Width="80" x:Name="Btn6" Click="Btn6_Click"/>

        <Button Content="7" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="436,342,0,0" Width="80" x:Name="Btn7" Click="Btn7_Click"/>

        <Button Content="8" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="536,342,0,0" Width="80" x:Name="Btn8" Click="Btn8_Click"/>

        <Button Content="9" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="636,342,0,0" Width="80" x:Name="Btn9" Click="Btn9_Click"/>

        <Button Content="0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="436,399,0,0" Width="80" x:Name="Btn0" Click="Btn0_Click"/>

        <Button Content="." HorizontalAlignment="Left" VerticalAlignment="Top" Margin="536,399,0,0" Width="80" x:Name="BtnDot" Click="BtnDot_Click"/>

        <Button Content="Cancel" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="636,399,0,0" Width="80" x:Name="BtnCan" Click="BtnCan_Click"/>

        <Button Content="=" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="736,342,0,0" Height="95" Width="80" x:Name="BtnEqual" Click="BtnEqual_Click"/>

        <Button Content="Per" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="736,231,0,0" Width="80" x:Name="BtnPer" Click="BtnPer_Click"/>

        <Button Content="Clear" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="736,287,0,0" Width="80" Click="Button_Click_1"/>     

    </Grid>

</Page>

Step 4

The "MainPage.xaml.cs" is as in the following code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace App2

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            ValuText.IsReadOnly = true;

        }

         string opration;

        double num1, num2, result;

        private void Btn1_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "1";

        }

        private void Btn2_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "2";

        }

        private void Btn3_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "3";

        }

        private void Btn4_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "4";

        }

        private void Btn5_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "5";

        }

        private void Btn6_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "6";

        }

        private void Btn7_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "7";

        }

        private void Btn8_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "8";

        }

        private void Btn9_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "9";

        } 

        private void Btn0_Click(object sender, RoutedEventArgs e)

        {

            TextBox.Text = TextBox.Text + "0";

        }

        private void BtnDot_Click(object sender, RoutedEventArgs e)

        {

            if(TextBox.Text.Contains("."))

            {

                TextBox.Text=TextBox.Text;

            }

            else

            {

                TextBox.Text=TextBox.Text+".";

            }

        }

        private void BtnPlus_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num1 = double.Parse(TextBox.Text);

                opration = "+";

                TextBox.Text = "";

                ValuText.Text = num1 + "+";

            }

        }

        private void BtnMinus_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num1 = double.Parse(TextBox.Text);

                opration = "-";

                TextBox.Text = "";

                ValuText.Text = num1 + "-";

            }

        }

        private void BtnMulti_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num1 = double.Parse(TextBox.Text);

                opration = "*";

                TextBox.Text = "";

                ValuText.Text = num1 + "*";

            }

        }

        private void BtnDiv_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num1 = double.Parse(TextBox.Text);

                opration = "/";

                TextBox.Text = "";

                ValuText.Text = num1 + "/";

            }

        }

        private void BtnPer_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num1 = double.Parse(TextBox.Text);

                opration = "%";

                TextBox.Text = "";

                ValuText.Text = num1 + "%";

            }

        } 

        private void BtnCan_Click(object sender, RoutedEventArgs e)

        {

            int length = TextBox.Text.Length;

            if (length > 0)

            {

                TextBox.Text = TextBox.Text.Remove(length - 1);

            }

            else

            {

                TextBox.Text = TextBox.Text;

            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

                TextBox.Text = "";

                ValuText.Text = "";

        } 

        private void BtnEqual_Click(object sender, RoutedEventArgs e)

        {

            if (TextBox.Text.Length > 0)

            {

                num2 = double.Parse(TextBox.Text);

                ValuText.Text = "";

                switch (opration)

                {

                    case "+":

                        result = num1 + num2;

                        TextBox.Text = result.ToString();

                        break;

                    case "-":

                        result = num1 - num2;

                        TextBox.Text = result.ToString();

                        break;

                    case "*":

                        result = num1 * num2;

                        TextBox.Text = result.ToString();

                        break;

                    case "/":

                        if (num2 == 0)

                        {

                            ValuText.Text = "Cannot divide by zero";

                            break;

                        }

                        else

                        {

                            result = num1 / num2;

                            TextBox.Text = result.ToString();

                            break;

                        }

                    case "%":

                        num1 = num1 * num2;

                        result = num1 / 100;

                        TextBox.Text = result.ToString();

                        break;

                }

            }

        }    

    }

}

Step 5

After running the program the output will look as in the following:

Cal-Result-Windows-Store-Apps.jpg