How To Create Calculator UWP App

Requirements: Visual Studio 2015 or higher version with Windows SDK.

Step 1: Open Visual Studio 2015 and afterwards, go to File and go to New. Now, go to project.

File→New→Project.

Project

Step 2: We can choose Visual C# and select Windows. Afterwards, select the universal and next select the Blank App(Universal Windows).

Visual C#→Windows→universal→Blank App(Universal Windows).

Blank App

Step 3: Choose the target and minimum platform versions, which your Universal Windows Application will support.

application

Step 4: Open Mainpage.XAML, add the codes given below:

 platform versions

The page is given below:

design

Step 5: Open Mainpage.cs file, add the code, given below:

  1. namespace calculator12  
  2. {  
  3.     /// <summary>  
  4.     /// An empty page that can be used on its own or navigated to within a Frame.  
  5.     /// </summary>  
  6.     public sealed partial class MainPage : Page  
  7.     {  
  8.         string input = string.Empty;  
  9.         string x = string.Empty;  
  10.         string y = string.Empty;  
  11.         char operation;  
  12.         double result = 0.0;  
  13.         public MainPage()  
  14.         {  
  15.             this.InitializeComponent();  
  16.         }  
  17.   
  18.         private void button1_Click(object sender, RoutedEventArgs e)  
  19.         {  
  20.             this.textBox.Text = " ";  
  21.             input += "1";  
  22.             this.textBox.Text += input;  
  23.         }  
  24.   
  25.         private void button_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             this.textBox.Text = " ";  
  28.             input += "2";  
  29.             this.textBox.Text += input;  
  30.   
  31.         }  
  32.   
  33.         private void button2_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             this.textBox.Text = " ";  
  36.             input += "3";  
  37.             this.textBox.Text += input;  
  38.   
  39.         }  
  40.   
  41.         private void button3_Click(object sender, RoutedEventArgs e)  
  42.         {  
  43.             this.textBox.Text = " ";  
  44.             input += "4";  
  45.             this.textBox.Text += input;  
  46.   
  47.         }  
  48.   
  49.         private void button4_Click(object sender, RoutedEventArgs e)  
  50.         {  
  51.             this.textBox.Text = " ";  
  52.             input += "5";  
  53.             this.textBox.Text += input;  
  54.   
  55.         }  
  56.   
  57.         private void button5_Click(object sender, RoutedEventArgs e)  
  58.         {  
  59.             this.textBox.Text = " ";  
  60.             input += "6";  
  61.             this.textBox.Text += input;  
  62.   
  63.         }  
  64.   
  65.         private void button6_Click(object sender, RoutedEventArgs e)  
  66.         {  
  67.             this.textBox.Text = " ";  
  68.             input += "7";  
  69.             this.textBox.Text += input;  
  70.   
  71.         }  
  72.   
  73.         private void button7_Click(object sender, RoutedEventArgs e)  
  74.         {  
  75.             this.textBox.Text = " ";  
  76.             input += "8";  
  77.             this.textBox.Text += input;  
  78.   
  79.         }  
  80.   
  81.         private void button8_Click(object sender, RoutedEventArgs e)  
  82.         {  
  83.             this.textBox.Text = " ";  
  84.             input += "9";  
  85.             this.textBox.Text += input;  
  86.   
  87.         }  
  88.   
  89.         private void button9_Click(object sender, RoutedEventArgs e)  
  90.         {  
  91.             this.textBox.Text = " ";  
  92.             input += "0";  
  93.             this.textBox.Text += input;  
  94.   
  95.         }  
  96.   
  97.         private void button10_Click(object sender, RoutedEventArgs e)  
  98.         {  
  99.             x = input;  
  100.             operation = '+';  
  101.             input = string.Empty;  
  102.         }  
  103.   
  104.         private void button11_Click(object sender, RoutedEventArgs e)  
  105.         {  
  106.             x = input;  
  107.             operation = '-';  
  108.             input = string.Empty;  
  109.         }  
  110.   
  111.         private void button12_Click(object sender, RoutedEventArgs e)  
  112.         {  
  113.             x = input;  
  114.             operation = '*';  
  115.             input = string.Empty;  
  116.         }  
  117.   
  118.         private void button13_Click(object sender, RoutedEventArgs e)  
  119.         {  
  120.             x = input;  
  121.             operation = '/';  
  122.             input = string.Empty;  
  123.         }  
  124.   
  125.         private void button14_Click(object sender, RoutedEventArgs e)  
  126.         {  
  127.   
  128.             this.textBox.Text = " ";  
  129.             input += ".";  
  130.             this.textBox.Text += input;  
  131.         }  
  132.   
  133.         private void button16_Click(object sender, RoutedEventArgs e)  
  134.         {  
  135.             y = input;  
  136.             double num1, num2;  
  137.   
  138.             double.TryParse(x, out num1);  
  139.             double.TryParse(y, out num2);  
  140.   
  141.             if (operation == '+')  
  142.             {  
  143.                 result = num1 + num2;  
  144.                 textBox.Text = result.ToString();  
  145.             }  
  146.             else if (operation == '-')  
  147.             {  
  148.                 result = num1 - num2;  
  149.                 textBox.Text = result.ToString();  
  150.             }  
  151.             else if (operation == '*')  
  152.             {  
  153.                 result = num1 * num2;  
  154.                 textBox.Text = result.ToString();  
  155.             }  
  156.             else if (operation == '/')  
  157.             {  
  158.                 if (num2 != 0)  
  159.                 {  
  160.                     result = num1 / num2;  
  161.                     textBox.Text = result.ToString();  
  162.                 }  
  163.                 else  
  164.                 {  
  165.                     textBox.Text = "DIV/Zero!";  
  166.                 }  
  167.   
  168.             }  
  169.         }  
  170.   
  171.         private void button15_Click(object sender, RoutedEventArgs e)  
  172.         {  
  173.             textBox.Text = "0";  
  174.         }  
  175.     }  
  176. }  
design

design

design

design

design

design

design

Step 6: You can run the project.

output

Step 7: You can see the output, given below:

output

output

 

Ebook Download
View all
Learn
View all