Metro Style Scientific Calculator

Introduction

In this article I will show you how to create a scientific calculator in Visual Studio 2012 with a Metro style. This scientific calculator can do the following operations.

Arithmetic Operations

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Percentage

Power Operations

  1. Square Root
  2. Square
  3. Cube
  4. Power

Logarithmic Operations

  1. Log on base 10
  2. Natural Log

Trigonometry Operations

  1. Sin 
  2. Cos
  3. Tan
  4. Inverse of Sine (sin-1)
  5. Inverse of Cos (cos-1)
  6. Inverse of Tan (tan-1)
  7. And you can also get Cosec, Sec and Cot value by using 1/X.

Permutations and Combinations

Factorial of any Number

Value of PI

Extra Features: Back Space Button, Auto Save History, Clear History, OFF Button, AC, CE
 
Use the following procedure to create it.

  1. Open Visual Studio
  2. Go to the menu and select "File" -> "New" -> "Project..." or simply press "Ctrl + Shift + N".

    New Project

  3. Now select "Blank App" inside Visual C#  Template and select "Blank App (XAML)".

    Blank App

  4. Now provide the name of the application and location.
  5. Now  go to the Solution Explorer and open MainPage.xaml Page and now start to make a calculator.

First of all make 11 buttons for 0-9 number buttons and one button for decimal (Dot(.)) using the following XAML code.

  1. <Button x:Name="BtnZero" Content="0" HorizontalAlignment="Left" Margin="406,643,0,0" VerticalAlignment="Top" Width="116" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>  
  2.   
  3.         <Button x:Name="Btn1" Content="1" HorizontalAlignment="Left" Margin="290,568,0,0" VerticalAlignment="Top" Width="117" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  4.   
  5.         <Button x:Name="Btn2" Content="2" HorizontalAlignment="Left" Margin="406,568,0,0" VerticalAlignment="Top" Width="116" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  6.   
  7.         <Button x:Name="Btn3" Content="3" HorizontalAlignment="Left" Margin="523,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  8.   
  9.         <Button x:Name="Btn4" Content="4" HorizontalAlignment="Left" Margin="290,493,0,0" VerticalAlignment="Top" Width="117" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  10.   
  11.         <Button x:Name="Btn5" Content="5" HorizontalAlignment="Left" Margin="406,493,0,0" VerticalAlignment="Top" Width="116" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  12.   
  13.         <Button x:Name="Btn6" Content="6" HorizontalAlignment="Left" Margin="523,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  14.   
  15. <Button x:Name="Btn7" Content="7" HorizontalAlignment="Left" Margin="290,418,0,0" VerticalAlignment="Top" Width="117" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  16.   
  17.         <Button x:Name="Btn8" Content="8" HorizontalAlignment="Left" Margin="406,418,0,0" VerticalAlignment="Top" Width="116" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  18.   
  19.         <Button x:Name="Btn9" Content="9" HorizontalAlignment="Left" Margin="523,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  20.   
  21.         <Button x:Name="BtnDot" Content="." HorizontalAlignment="Left" Margin="290,643,0,0" VerticalAlignment="Top" Width="117" Background="Black" Height="74" BorderBrush="Red" FontSize="36" />  
Now by using code above the buttons look like:
 
Buttons code
 
Now we will create a TextBox that will take input by the click event of the button. 
  1. <TextBox x:Name="TxtBox" HorizontalAlignment="Left" Margin="10,118,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="1346" Height="89" FontSize="48" FontWeight="Bold" FlowDirection="RightToLeft" BorderBrush="Black" BorderThickness="4" Text="" MaxLength="10"/>  
Now create buttons for arithmetic operations using the following code:
  1. <Button x:Name="BtnAdd" Content="+" HorizontalAlignment="Left" Margin="637,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="149" BorderBrush="Red" FontSize="36" />  
  2.   
  3. <Button x:Name="BtnMul" Content="*" HorizontalAlignment="Left" Margin="637,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36"/>  
  4.   
  5. <Button x:Name="BtnSub" Content="-" HorizontalAlignment="Left" Margin="637,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  6.   
  7. <Button x:Name="BtnDiv" Content="/" HorizontalAlignment="Left" Margin="752,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36"/>  
  8.  
  9. <Button x:Name="BtnEqual" Content="=" HorizontalAlignment="Left" Margin="523,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>  
  10. <Button x:Name="BtnPercent" Content="%" HorizontalAlignment="Left" Margin="752,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36"/>
The Above will generate Addition Button(+), Subtraction Button(-), Multiplication Button(*), Division Button(/), Equals Button (=) and Percentage button(%) 

Now create buttons for power operations using the following code:

  1. <Button x:Name="BtnSquare" Content="X^2" HorizontalAlignment="Left" Margin="867,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  2.   
  3.         <Button x:Name="BtnCube" Content="X^3" HorizontalAlignment="Left" Margin="867,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36"/>  
  4.   
  5.         <Button x:Name="BtnRoot" Content="√" HorizontalAlignment="Left" Margin="752,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>  
  6.   
  7.         <Button x:Name="BtnPow" Content="X^Y" HorizontalAlignment="Left" Margin="982,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36"/>  
The code above will generate the following buttons: Square, Cube, Square Root and Power.
To create the Square Root button:
  1. Open "Run" by pressing "Windows Key + R".
  2. And enter "charmap". This command will open the Character Map.

    Character Map

  3. After pressing "OK" you will see the following Dialog Box:

    Dialog Box

  4. Now search for the Square Root Symbol character then select that and copy that.

     Square Root Symbol character

  5. Now paste the copied character into your program.
Now create buttons for logarithmic operations using the following code:
  1.  <Button x:Name="BtnLog" Content="Log" HorizontalAlignment="Left" Margin="867,567,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  2.   
  3.         <Button x:Name="BtnLN" Content="ln" HorizontalAlignment="Left" Margin="867,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
Now create buttons for trigonometry operations using the following code:
  1. <Button x:Name="BtnSin" Content="sin" HorizontalAlignment="Left" Margin="1094,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  2.   
  3. <Button x:Name="BtnCos" Content="cos" HorizontalAlignment="Left" Margin="1094,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  4.   
  5. <Button x:Name="BtnTan" Content="tan" HorizontalAlignment="Left" Margin="1094,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  6.   
  7. <Button x:Name="BtnSinInv" Content="sin-1" HorizontalAlignment="Left" Margin="1209,418,0,0" VerticalAlignment="Top" Width="131" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  8.   
  9. <Button x:Name="BtnCosInv" Content="cos-1" HorizontalAlignment="Left" Margin="1209,493,0,0" VerticalAlignment="Top" Width="131" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  10.   
  11. <Button x:Name="BtnTanInv" Content="tan-1" HorizontalAlignment="Left" Margin="1209,568,0,0" VerticalAlignment="Top" Width="131" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
Now create buttons for permutations, combinations and factorials operations using the following code:
  1. <Button x:Name="BtnNCR" Content="nCr" HorizontalAlignment="Left" Margin="982,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36" />  
  2.   
  3.         <Button x:Name="BtnNPR" Content="nPr" HorizontalAlignment="Left" Margin="982,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  4.   
  5.         <Button x:Name="BtnFact" Content="X!" HorizontalAlignment="Left" Margin="1094,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36" />  

Now create buttons for +/-, PI and 1/X using the following code:

  1. <Button x:Name="BtnPlusMinus" Content="±" HorizontalAlignment="Left" Margin="752,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  2.   
  3.         <Button x:Name="Btn1byX" Content="1/X" HorizontalAlignment="Left" Margin="982,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>  
  4.   
  5.         <Button x:Name="BtnPI" Content="π" HorizontalAlignment="Left" Margin="1209,643,0,0" VerticalAlignment="Top" Width="131" Background="Black" Height="74" BorderBrush="Red" FontSize="36" />  

Now create buttons for backspace, AC, CE, OFF and Clear History using the following code:

  1. <Button x:Name="BtnBackSpace" Content="BACK SPACE(←)" HorizontalAlignment="Left" Margin="982,342,0,0" VerticalAlignment="Top" Width="358" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  2.   
  3.         <Button x:Name="BtnAc" Content="AC" HorizontalAlignment="Left" Margin="867,342,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  4.   
  5.         <Button x:Name="BtnCE" Content="CE" HorizontalAlignment="Left" Margin="752,342,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" RenderTransformOrigin="0.682,0.527"/>  
  6.   
  7.         <Button x:Name="BtnOff" Content="OFF" HorizontalAlignment="Left" Margin="637,342,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />  
  8.   
  9.        <Button x:Name="BtnClearHistory" Content="Clear History" HorizontalAlignment="Left" Margin="291,342,0,0" VerticalAlignment="Top" Width="346" Background="Black" Height="76" BorderBrush="Red" FontSize="36"/>  
Now make a List-Box that will store the history using the following code:
  1. <ListBox x:Name="History" HorizontalAlignment="Left" Height="375" Margin="10,342,0,0" VerticalAlignment="Top" Width="275" FontSize="24" FontWeight="Bold" Background="#CCFFFFFF" Foreground="Black" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Padding="0" />  
Now writing the entire code above you can see in your designer that it will look such as the following:

Designer Window 
 
Now inside the code behind you need to write the following code:
  1. public sealed partial class MainPage : Page  
  2.     {  
  3.         public bool status;//It checks the status that what you want to append the string into the textbox or clear the textbox  
  4.         public bool Dot;// it checks that only one Dot You can put inside the textbox  
  5.         public double PreviousValue = 0.0;// by default previous value will be 0.0 and on performing any operation it will be change according to the textbox  
  6.         public char Operation;//this checks which operation you want to perform like add, subtract, etc...  
  7.         public bool FirstTime = true;  
  8.         public MainPage()  
  9.         {  
  10.             this.InitializeComponent();  
  11.             TxtBox.IsReadOnly = true;// textbox will be ready only no one can write inside the textbox  
  12.             TxtBox.Text = "0";// default value of the textbox  
  13.         }  
Now write the following code for the number buttons click event.
  1. #region Number Buttons  
  2.         private void BtnZero_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             if (status == true || BtnZero.Content.ToString() != "0")  
  5.             {  
  6.                 TxtBox.Text += BtnZero.Content;  
  7.             }  
  8.             else  
  9.             {  
  10.                 TxtBox.Text = BtnZero.Content.ToString();  
  11.             }  
  12.         }  
  13.   
  14.         private void Btn1_Click(object sender, RoutedEventArgs e)  
  15.         {  
  16.             if (status == true)  
  17.             {  
  18.                 TxtBox.Text += Btn1.Content;  
  19.             }  
  20.             else  
  21.             {  
  22.                 TxtBox.Text = Btn1.Content.ToString();  
  23.                 status = true;  
  24.             }  
  25.         }  
  26.   
  27.         private void Btn2_Click(object sender, RoutedEventArgs e)  
  28.         {  
  29.             if (status == true)  
  30.             {  
  31.                 TxtBox.Text += Btn2.Content;  
  32.             }  
  33.             else  
  34.             {  
  35.                 TxtBox.Text = Btn2.Content.ToString();  
  36.                 status = true;  
  37.             }  
  38.         }  
  39.   
  40.         private void Btn3_Click(object sender, RoutedEventArgs e)  
  41.         {  
  42.             if (status == true)  
  43.             {  
  44.                 TxtBox.Text += Btn3.Content;  
  45.             }  
  46.             else  
  47.             {  
  48.                 TxtBox.Text = Btn3.Content.ToString();  
  49.                 status = true;  
  50.             }  
  51.         }  
  52.   
  53.         private void Btn4_Click(object sender, RoutedEventArgs e)  
  54.         {  
  55.             if (status == true)  
  56.             {  
  57.                 TxtBox.Text += Btn4.Content;  
  58.             }  
  59.             else  
  60.             {  
  61.                 TxtBox.Text = Btn4.Content.ToString();  
  62.                 status = true;  
  63.             }  
  64.         }  
  65.   
  66.         private void Btn5_Click(object sender, RoutedEventArgs e)  
  67.         {  
  68.             if (status == true)  
  69.             {  
  70.                 TxtBox.Text += Btn5.Content;  
  71.             }  
  72.             else  
  73.             {  
  74.                 TxtBox.Text = Btn5.Content.ToString();  
  75.                 status = true;  
  76.             }  
  77.         }  
  78.   
  79.         private void Btn6_Click(object sender, RoutedEventArgs e)  
  80.         {  
  81.             if (status == true)  
  82.             {  
  83.                 TxtBox.Text += Btn6.Content;  
  84.             }  
  85.             else  
  86.             {  
  87.                 TxtBox.Text = Btn6.Content.ToString();  
  88.                 status = true;  
  89.             }  
  90.         }  
  91.   
  92.         private void Btn7_Click(object sender, RoutedEventArgs e)  
  93.         {  
  94.             if (status == true)  
  95.             {  
  96.                 TxtBox.Text += Btn7.Content;  
  97.             }  
  98.             else  
  99.             {  
  100.                 TxtBox.Text = Btn7.Content.ToString();  
  101.                 status = true;  
  102.             }  
  103.         }  
  104.   
  105.         private void Btn8_Click(object sender, RoutedEventArgs e)  
  106.         {  
  107.             if (status == true)  
  108.             {  
  109.                 TxtBox.Text += Btn8.Content;  
  110.             }  
  111.             else  
  112.             {  
  113.                 TxtBox.Text = Btn8.Content.ToString();  
  114.                 status = true;  
  115.             }  
  116.         }  
  117.   
  118.         private void Btn9_Click(object sender, RoutedEventArgs e)  
  119.         {  
  120.             if (status == true)  
  121.             {  
  122.                 TxtBox.Text += Btn9.Content;  
  123.             }  
  124.             else  
  125.             {  
  126.                 TxtBox.Text = Btn9.Content.ToString();  
  127.                 status = true;  
  128.             }  
  129.         }  
  130.         #endregion Number Buttons  
For the Dot button's click event you can write the following code:
  1. //For Dot(.) Button  
  2.         private void BtnDot_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             if (Dot == false)  
  5.             {  
  6.                 TxtBox.Text += BtnDot.Content.ToString();  
  7.                 Dot = true;  
  8.                 status = true;  
  9.             }  
  10.         }  
For the arithmetic operations write the following code:
  1. #region Arithmatic Operation  
  2.         private void BtnAdd_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             if (!FirstTime)  
  5.             {  
  6.                 OpreationMethod();  
  7.                 Operation = '+';  
  8.                 status = false;  
  9.             }  
  10.             else  
  11.             {  
  12.                 PreviousValue = double.Parse(TxtBox.Text);  
  13.                 Operation = '+';  
  14.                 status = false;  
  15.                 FirstTime = false;  
  16.             }  
  17.             TxtBox.Text = "0";  
  18.         }  
  19.   
  20.         private void BtnSub_Click(object sender, RoutedEventArgs e)  
  21.         {  
  22.             if (!FirstTime)  
  23.             {  
  24.                 OpreationMethod();  
  25.                 Operation = '-';  
  26.                 status = false;  
  27.             }  
  28.             else  
  29.             {  
  30.                 PreviousValue = double.Parse(TxtBox.Text);  
  31.                 Operation = '-';  
  32.                 status = false;  
  33.                 FirstTime = false;  
  34.             }  
  35.             TxtBox.Text = "0";  
  36.         }  
  37.   
  38.         private void BtnMul_Click(object sender, RoutedEventArgs e)  
  39.         {  
  40.             if (!FirstTime)  
  41.             {  
  42.                 OpreationMethod();  
  43.                 Operation = '*';  
  44.                 status = false;  
  45.             }  
  46.             else  
  47.             {  
  48.                 PreviousValue = double.Parse(TxtBox.Text);  
  49.                 Operation = '*';  
  50.                 status = false;  
  51.                 FirstTime = false;  
  52.             }  
  53.             TxtBox.Text = "0";  
  54.         }  
  55.   
  56.         private void BtnDiv_Click(object sender, RoutedEventArgs e)  
  57.         {  
  58.             if (!FirstTime)  
  59.             {  
  60.                 OpreationMethod();  
  61.                 Operation = '/';  
  62.                 status = false;  
  63.             }  
  64.             else  
  65.             {  
  66.                 PreviousValue = double.Parse(TxtBox.Text);  
  67.                 Operation = '/';  
  68.                 status = false;  
  69.                 FirstTime = false;  
  70.             }  
  71.             TxtBox.Text = "0";  
  72.         }  
The following is the code for the equals button's click event:
  1. //When User Will Click On Equal Button(=)  
  2.         private void BtnEqual_Click(object sender, RoutedEventArgs e)  
  3.         {     
  4.             if (Operation != '\0')  
  5.             {  
  6.                 OpreationMethod();  
  7.                 TxtBox.Text = PreviousValue.ToString();  
  8.                 PreviousValue = 0.0;  
  9.             }  
  10.             status = false;  
  11.             FirstTime = true;  
  12.             Operation = '\0';  
  13.         }  
The following is the code for the Click event of the calculator's OFF, AC and CE buttons:
  1. //Calulator Off  
  2.         private void BtnOff_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             BtnZero.IsEnabled = false;  
  5.             Btn1.IsEnabled = false;  
  6.             Btn2.IsEnabled = false;  
  7.             Btn3.IsEnabled = false;  
  8.             Btn4.IsEnabled = false;  
  9.             Btn5.IsEnabled = false;  
  10.             Btn6.IsEnabled = false;  
  11.             Btn7.IsEnabled = false;  
  12.             Btn8.IsEnabled = false;  
  13.             Btn9.IsEnabled = false;  
  14.             Btn1byX.IsEnabled = false;  
  15.             BtnAdd.IsEnabled = false;  
  16.             BtnBackSpace.IsEnabled = false; ;  
  17.             BtnCE.IsEnabled = false;  
  18.             BtnCos.IsEnabled = false; ;  
  19.             BtnCosInv.IsEnabled = false;  
  20.             BtnCube.IsEnabled = false;  
  21.             BtnDiv.IsEnabled = false;  
  22.             BtnDot.IsEnabled = false;  
  23.             BtnEqual.IsEnabled = false;  
  24.             BtnFact.IsEnabled = false;  
  25.             BtnLN.IsEnabled = false;  
  26.             BtnLog.IsEnabled = false;  
  27.             BtnMul.IsEnabled = false;  
  28.             BtnNCR.IsEnabled = false; ;  
  29.             BtnNPR.IsEnabled = false;  
  30.             BtnPercent.IsEnabled = false;  
  31.             BtnPI.IsEnabled = false;  
  32.             BtnPlusMinus.IsEnabled = false;  
  33.             BtnPow.IsEnabled = false;  
  34.             BtnRoot.IsEnabled = false;  
  35.             BtnSin.IsEnabled = false;  
  36.             BtnSinInv.IsEnabled = false;  
  37.             BtnSquare.IsEnabled = false;  
  38.             BtnTan.IsEnabled = false;  
  39.             BtnTanInv.IsEnabled = false;  
  40.             BtnSub.IsEnabled = false;  
  41.   
  42.             PreviousValue = 0.0;  
  43.             TxtBox.Text = string.Empty;  
  44.             Dot = false;  
  45.             FirstTime = true;  
  46.         }  
  47.         //Calculator ON or Reset all  
  48.         private void BtnAc_Click(object sender, RoutedEventArgs e)  
  49.         {  
  50.             BtnZero.IsEnabled = true;  
  51.             Btn1.IsEnabled = true;  
  52.             Btn2.IsEnabled = true;  
  53.             Btn3.IsEnabled = true;  
  54.             Btn4.IsEnabled = true;  
  55.             Btn5.IsEnabled = true;  
  56.             Btn6.IsEnabled = true;  
  57.             Btn7.IsEnabled = true;  
  58.             Btn8.IsEnabled = true;  
  59.             Btn9.IsEnabled = true;  
  60.             Btn1byX.IsEnabled = true;  
  61.             BtnAdd.IsEnabled = true;  
  62.             BtnBackSpace.IsEnabled = true;  
  63.             BtnCE.IsEnabled = true;  
  64.             BtnCos.IsEnabled = true; ;  
  65.             BtnCosInv.IsEnabled = true;  
  66.             BtnCube.IsEnabled = true;  
  67.             BtnDiv.IsEnabled = true;  
  68.             BtnDot.IsEnabled = true;  
  69.             BtnEqual.IsEnabled = true;  
  70.             BtnFact.IsEnabled = true;  
  71.             BtnLN.IsEnabled = true;  
  72.             BtnLog.IsEnabled = true;  
  73.             BtnMul.IsEnabled = true;  
  74.             BtnNCR.IsEnabled = true; ;  
  75.             BtnNPR.IsEnabled = true;  
  76.             BtnPercent.IsEnabled = true;  
  77.             BtnPI.IsEnabled = true;  
  78.             BtnPlusMinus.IsEnabled = true;  
  79.             BtnPow.IsEnabled = true;  
  80.             BtnRoot.IsEnabled = true;  
  81.             BtnSin.IsEnabled = true;  
  82.             BtnSinInv.IsEnabled = true;  
  83.             BtnSquare.IsEnabled = true;  
  84.             BtnTan.IsEnabled = true;  
  85.             BtnTanInv.IsEnabled = true;  
  86.             BtnSub.IsEnabled = true;  
  87.   
  88.             PreviousValue = 0.0;  
  89.             TxtBox.Text = "0";  
  90.             Dot = false;  
  91.             FirstTime = true;  
  92.         }  
  93. //Clear last  
  94.         private void BtnCE_Click(object sender, RoutedEventArgs e)  
  95.         {  
  96.             TxtBox.Text = "0";  
  97.         }  
The following is the code for the Click Event of the Clear History button:
  1. //Delete History  
  2.         private void BtnClearHistory_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             History.Items.Clear();  
  5.         }  
The following is the code for Backspace button's Click Event:
  1. private void BtnBackSpace_Click(object sender, RoutedEventArgs e)  
  2.        {  
  3.            if (TxtBox.Text != "0")  
  4.            {  
  5.                string str = TxtBox.Text;  
  6.                int n = str.Length;  
  7.                TxtBox.Text = (str.Substring(0, n - 1));  
  8.            }  
  9.            if (TxtBox.Text.Length == 0)  
  10.            {  
  11.                TxtBox.Text = "0";  
  12.                status = false;  
  13.            }  
  14.        }   
The following is the code for the Logarithmic (for the  Click Event of the Log and ln buttons) function:
  1. //Log Based On 10  
  2.         private void BtnLog_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             Operation = 'l';  
  5.             OpreationMethod();  
  6.         }  
  7.         //Natural Log  
  8.         private void BtnLN_Click(object sender, RoutedEventArgs e)  
  9.         {  
  10.             Operation = 'n';  
  11.             OpreationMethod();  
  12.         }  
The following is the code for Power function (for the  Click Event of the X^2, X^3 and X^Y buttons):
  1. //Power Funtion X^Y  
  2. private void BtnPow_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             if (!FirstTime)  
  5.             {  
  6.                 OpreationMethod();  
  7.                 Operation = '^';  
  8.                 status = false;  
  9.             }  
  10.             else  
  11.             {  
  12.                 PreviousValue = double.Parse(TxtBox.Text);  
  13.                 Operation = '^';  
  14.                 status = false;  
  15.                 FirstTime = false;  
  16.             }  
  17.             TxtBox.Text = "0";  
  18.         }  
  19.   
  20.         //Square Find  
  21.         private void BtnSquare_Click(object sender, RoutedEventArgs e)  
  22.         {  
  23.             Operation = '2';  
  24.             OpreationMethod();  
  25.         }  
  26.   
  27. //Cube Find  
  28.         private void BtnCube_Click(object sender, RoutedEventArgs e)  
  29.         {  
  30.             Operation = '3';  
  31.             OpreationMethod();  
  32.         }  
The following is the code for Trigonometry  functions(for the  Click Event of the sin, cos, tan, sin-1, cos-1 and  tan-1 button's click event)
  1. //Sin  
  2.         private void BtnSin_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             Operation = 'S';  
  5.             OpreationMethod();  
  6.         }  
  7.         //Cos  
  8.         private void BtnCos_Click(object sender, RoutedEventArgs e)  
  9.         {  
  10.             Operation = 'C';  
  11.             OpreationMethod();  
  12.         }  
  13.         //Tan  
  14.         private void BtnTan_Click(object sender, RoutedEventArgs e)  
  15.         {  
  16.             Operation = 'T';  
  17.             OpreationMethod();  
  18.         }  
  19.         //Sine Inverse  
  20.         private void BtnSinInv_Click(object sender, RoutedEventArgs e)  
  21.         {  
  22.             Operation = 's';  
  23.             OpreationMethod();  
  24.         }  
  25.         //Cos Inverse  
  26.         private void BtnCosInv_Click(object sender, RoutedEventArgs e)  
  27.         {  
  28.             Operation = 'c';  
  29.             OpreationMethod();  
  30.         }  
  31.         //Tan Inverse  
  32.         private void BtnTanInv_Click(object sender, RoutedEventArgs e)  
  33.         {  
  34.             Operation = 't';  
  35.             OpreationMethod();  
  36.         }  
The following is the code for Factorial, Permutations and Combinations (for the  Click Event of the X!, nPr and nCr buttons):
  1.  //Factorial  
  2.         private void BtnFact_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             Operation = 'f';  
  5.             OpreationMethod();  
  6.         }  
  7. //Find NCR (Combination)  
  8.         private void BtnNCR_Click(object sender, RoutedEventArgs e)  
  9.         {  
  10.             if (!FirstTime)  
  11.             {  
  12.                 OpreationMethod();  
  13.                 Operation = 'R';  
  14.                 status = false;  
  15.             }  
  16.             else  
  17.             {  
  18.                 PreviousValue = double.Parse(TxtBox.Text);  
  19.                 Operation = 'R';  
  20.                 status = false;  
  21.                 FirstTime = false;  
  22.             }  
  23.             TxtBox.Text = "0";  
  24.         }  
  25.         //Find NPR (Permutation)  
  26.         private void BtnNPR_Click(object sender, RoutedEventArgs e)  
  27.         {  
  28.             if (!FirstTime)  
  29.             {  
  30.                 OpreationMethod();  
  31.                 Operation = 'r';  
  32.                 status = false;  
  33.             }  
  34.             else  
  35.             {  
  36.                 PreviousValue = double.Parse(TxtBox.Text);  
  37.                 Operation = 'r';  
  38.                 status = false;  
  39.                 FirstTime = false;  
  40.             }  
  41.             TxtBox.Text = "0";  
  42.         }  
The following is the code for the  Click Event of the PI, +/- and 1/X buttons:
  1. //value of PI  
  2. private void BtnPI_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             Operation = 'p';  
  5.             OpreationMethod();  
  6.         }  
  7.   
  8. //Plus and Minus  
  9.         private void BtnPlusMinus_Click(object sender, RoutedEventArgs e)  
  10.         {  
  11.             TxtBox.Text = (-1.0 * double.Parse(TxtBox.Text)).ToString();  
  12.         }  
  13.   
  14. //1/X  
  15.         private void Btn1byX_Click(object sender, RoutedEventArgs e)  
  16.         {  
  17.             Operation = 'o';  
  18.             OpreationMethod();  
  19.         }  
The following is the code for the Click Event of the Square Root Button:
  1. private void BtnRoot_Click(object sender, RoutedEventArgs e)  
  2.        {  
  3.            Operation = 'q';  
  4.            OpreationMethod();  
  5.        }  
The following is the code for the Click Event of the Percentage Buttons:
  1. //For finding percentage (%)  
  2.         private void BtnPercent_Click(object sender, RoutedEventArgs e)  
  3.         {  
  4.             TxtBox.Text = (PreviousValue * double.Parse(TxtBox.Text) / 100).ToString();  
  5.         }  
Note that in all the events I am calling the function "OpreationMethod." In this method I am performing all the operations using the "Operation" variable that is a character and I have defined the above at the class lavel. The code of the "OpreationMethod" function is given below.
  1. private void OpreationMethod()  
  2.         {  
  3.             switch (Operation)  
  4.             {  
  5.                 case '+':  
  6.                     {  
  7.                         History.Items.Add(PreviousValue + " + " + TxtBox.Text + "=" + (PreviousValue + double.Parse(TxtBox.Text)));  
  8.                         PreviousValue += double.Parse(TxtBox.Text);  
  9.                         break;  
  10.                     }  
  11.                 case '-':  
  12.                     {  
  13.                         History.Items.Add(PreviousValue + " - " + TxtBox.Text + "=" + (PreviousValue - double.Parse(TxtBox.Text)));  
  14.                         PreviousValue -= double.Parse(TxtBox.Text);  
  15.                         break;  
  16.                     }  
  17.                 case '*':  
  18.                     {  
  19.                         History.Items.Add(PreviousValue + " * " + TxtBox.Text + "=" + (PreviousValue * double.Parse(TxtBox.Text)));  
  20.                         PreviousValue *= double.Parse(TxtBox.Text);  
  21.                         break;  
  22.                     }  
  23.                 case '/':  
  24.                     {  
  25.                         History.Items.Add(PreviousValue + " / " + TxtBox.Text + "=" + (PreviousValue / double.Parse(TxtBox.Text)));  
  26.                         PreviousValue /= double.Parse(TxtBox.Text);  
  27.                         break;  
  28.                     }  
  29.                 case 'q':  
  30.                     {  
  31.                         double sqrt = Math.Sqrt(double.Parse(TxtBox.Text));  
  32.                         History.Items.Add("sqrt(" + TxtBox.Text + ") = " + sqrt);  
  33.                         PreviousValue = sqrt;  
  34.                         TxtBox.Text = PreviousValue.ToString();  
  35.                         status = false;  
  36.                         break;  
  37.                     }  
  38.                 case '3':  
  39.                     {  
  40.                         double cube = Math.Pow(double.Parse(TxtBox.Text), 3);  
  41.                         History.Items.Add("cube(" + TxtBox.Text + ") = " + cube);  
  42.                         PreviousValue = cube;  
  43.                         TxtBox.Text = PreviousValue.ToString();  
  44.                         status = false;  
  45.                         break;  
  46.                     }  
  47.                 case '2':  
  48.                     {  
  49.                         double square = Math.Pow(double.Parse(TxtBox.Text), 2);  
  50.                         History.Items.Add("square(" + TxtBox.Text + ") = " + square);  
  51.                         PreviousValue = square;  
  52.                         TxtBox.Text = PreviousValue.ToString();  
  53.                         status = false;  
  54.                         break;  
  55.                     }  
  56.                 case 'l':  
  57.                     {  
  58.                         double log10 = Math.Log10(double.Parse(TxtBox.Text));  
  59.                         History.Items.Add("Log(" + TxtBox.Text + ") = " + log10);  
  60.                         PreviousValue = log10;  
  61.                         TxtBox.Text = PreviousValue.ToString();  
  62.                         status = false;  
  63.                         break;  
  64.                     }  
  65.                 case 'n':  
  66.                     {  
  67.                         double LN = Math.Log(double.Parse(TxtBox.Text));  
  68.                         History.Items.Add("LN(" + TxtBox.Text + ") = " + LN);  
  69.                         PreviousValue = LN;  
  70.                         TxtBox.Text = PreviousValue.ToString();  
  71.                         status = false;  
  72.                         break;  
  73.                     }  
  74.                 case '^':  
  75.                     {  
  76.                         double Pow = Math.Pow(PreviousValue, double.Parse(TxtBox.Text));  
  77.                         History.Items.Add(PreviousValue + " ^ " + TxtBox.Text + "=" + Pow);  
  78.                         PreviousValue = Pow;  
  79.                         break;  
  80.                     }  
  81.                 case 'o':  
  82.                     {  
  83.                         double inverse = 1.0 / double.Parse(TxtBox.Text);  
  84.                         History.Items.Add("1/(" + TxtBox.Text + ") = " + inverse);  
  85.                         PreviousValue = inverse;  
  86.                         TxtBox.Text = PreviousValue.ToString();  
  87.                         status = false;  
  88.                         break;  
  89.                     }  
  90.                 case 'f':  
  91.                     {  
  92.                         double Fact = 1;  
  93.                         for (int i = 1; i <= Convert.ToInt32(TxtBox.Text); i++)  
  94.                         {  
  95.                             Fact = Fact * i;  
  96.                         }  
  97.                         History.Items.Add(TxtBox.Text + "! = " + Fact);  
  98.                         PreviousValue = Fact;  
  99.                         TxtBox.Text = PreviousValue.ToString();  
  100.                         status = false;  
  101.                         break;  
  102.                     }  
  103.                 case 'p':  
  104.                     {  
  105.                         double PI = Math.PI;  
  106.                         History.Items.Add("Value of π = " + PI);  
  107.                         PreviousValue = PI;  
  108.                         TxtBox.Text = PreviousValue.ToString();  
  109.                         status = false;  
  110.                         break;  
  111.                     }  
  112.                 case 'S':  
  113.                     {  
  114.                         double Sin = Math.Sin(double.Parse(TxtBox.Text));  
  115.                         History.Items.Add("Sin(" + TxtBox.Text + ") = " + Sin);  
  116.                         PreviousValue = Sin;  
  117.                         TxtBox.Text = PreviousValue.ToString();  
  118.                         status = false;  
  119.                         break;  
  120.                     }  
  121.                 case 'C':  
  122.                     {  
  123.                         double Cos = Math.Cos(double.Parse(TxtBox.Text));  
  124.                         History.Items.Add("Cos(" + TxtBox.Text + ") = " + Cos);  
  125.                         PreviousValue = Cos;  
  126.                         TxtBox.Text = PreviousValue.ToString();  
  127.                         status = false;  
  128.                         break;  
  129.                     }  
  130.                 case 'T':  
  131.                     {  
  132.                         double Tan = Math.Tan(double.Parse(TxtBox.Text));  
  133.                         History.Items.Add("Tan(" + TxtBox.Text + ") = " + Tan);  
  134.                         PreviousValue = Tan;  
  135.                         TxtBox.Text = PreviousValue.ToString();  
  136.                         status = false;  
  137.                         break;  
  138.                     }  
  139.                 case 's':  
  140.                     {  
  141.                         double SinInv = Math.Asin(double.Parse(TxtBox.Text));  
  142.                         History.Items.Add("sin-1(" + TxtBox.Text + ") = " + SinInv);  
  143.                         PreviousValue = SinInv;  
  144.                         TxtBox.Text = PreviousValue.ToString();  
  145.                         status = false;  
  146.                         break;  
  147.                     }  
  148.                 case 'c':  
  149.                     {  
  150.                         double CosInv = Math.Acos(double.Parse(TxtBox.Text));  
  151.                         History.Items.Add("cos-1(" + TxtBox.Text + ") = " + CosInv);  
  152.                         PreviousValue = CosInv;  
  153.                         TxtBox.Text = PreviousValue.ToString();  
  154.                         status = false;  
  155.                         break;  
  156.                     }  
  157.                 case 't':  
  158.                     {  
  159.                         double TanInv = Math.Acos(double.Parse(TxtBox.Text));  
  160.                         History.Items.Add("tan-1(" + TxtBox.Text + ") = " + TanInv);  
  161.                         PreviousValue = TanInv;  
  162.                         TxtBox.Text = PreviousValue.ToString();  
  163.                         status = false;  
  164.                         break;  
  165.                     }  
  166.                 case 'r':  
  167.                     {  
  168.                         int var1, var2;  
  169.                         var1 = factorial(Convert.ToInt32(PreviousValue));  
  170.                         var2 = factorial(Convert.ToInt32(PreviousValue) - Convert.ToInt32(TxtBox.Text));  
  171.                         History.Items.Add(PreviousValue + "P" + TxtBox.Text + " = " + Convert.ToString(var1 / var2));  
  172.                         PreviousValue = double.Parse(Convert.ToString(var1 / var2));  
  173.                         TxtBox.Text = PreviousValue.ToString();  
  174.                         status = false;  
  175.                         break;  
  176.                     }  
  177.   
  178.                 case 'R':  
  179.                     {  
  180.                         int var1, var2, var3;  
  181.                         var1 = factorial(Convert.ToInt32(PreviousValue));  
  182.                         var2 = factorial(Convert.ToInt32(PreviousValue) - Convert.ToInt32(TxtBox.Text));  
  183.                         var3 = factorial(Convert.ToInt32(TxtBox.Text));  
  184.                         History.Items.Add(PreviousValue + "C" + TxtBox.Text + " = " + Convert.ToString(var1 / (var3 * var2)));  
  185.                         PreviousValue = double.Parse(Convert.ToString(var1 / (var3 * var2)));  
  186.                         TxtBox.Text = PreviousValue.ToString();  
  187.                         status = false;  
  188.                         break;  
  189.                     }  
  190.                 default:  
  191.                     {  
  192.                         break;  
  193.                     }  
  194.             }  
  195.         }  
I have created one factorial function that finds the factorial as given below:
  1. private int factorial(int x)  
  2.         {  
  3.             int i = 1;  
  4.             for (int s = 1; s <= x; s++)  
  5.             {  
  6.                 i = i * s;  
  7.             }  
  8.             return i;  
  9.         }  
After doing this I am adding a logo of the application so I have created one logo. You can also create it use the following to add the logo.
  1. Open Solution Explorer.
  2. Open the Package.appxmanifest file.
  3. Go to the logo tab and provide the logo such as the following:

     logo tab
Now after writing all the code, build the project and and run it. After running this you will see the output as follows:
  1. Output in Start Menu.

     Start Menu

  2. Output after running the project.

    running the project

  3. Output of calculator:

    calculator


Similar Articles