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
- Addition
- Subtraction
- Multiplication
- Division
- Percentage
Power Operations
- Square Root
- Square
- Cube
- Power
Logarithmic Operations
- Log on base 10
- Natural Log
Trigonometry Operations
- Sin
- Cos
- Tan
- Inverse of Sine (sin-1)
- Inverse of Cos (cos-1)
- Inverse of Tan (tan-1)
- 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.
- Open Visual Studio
- Go to the menu and select "File" -> "New" -> "Project..." or simply press "Ctrl + Shift + N".
![New Project]()
- Now select "Blank App" inside Visual C# Template and select "Blank App (XAML)".
![Blank App]()
- Now provide the name of the application and location.
- 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.
- <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"/>
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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.
- <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:
- <Button x:Name="BtnAdd" Content="+" HorizontalAlignment="Left" Margin="637,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="149" BorderBrush="Red" FontSize="36" />
-
- <Button x:Name="BtnMul" Content="*" HorizontalAlignment="Left" Margin="637,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="75" BorderBrush="Red" FontSize="36"/>
-
- <Button x:Name="BtnSub" Content="-" HorizontalAlignment="Left" Margin="637,493,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />
-
- <Button x:Name="BtnDiv" Content="/" HorizontalAlignment="Left" Margin="752,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36"/>
-
- <Button x:Name="BtnEqual" Content="=" HorizontalAlignment="Left" Margin="523,643,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>
- <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:
- <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" />
-
- <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"/>
-
- <Button x:Name="BtnRoot" Content="√" HorizontalAlignment="Left" Margin="752,418,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="74" BorderBrush="Red" FontSize="36"/>
-
- <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:
- Open "Run" by pressing "Windows Key + R".
- And enter "charmap". This command will open the Character Map.
![Character Map]()
- After pressing "OK" you will see the following Dialog Box:
![Dialog Box]()
- Now search for the Square Root Symbol character then select that and copy that.
![Square Root Symbol character]()
- Now paste the copied character into your program.
Now create buttons for logarithmic operations using the following code:
- <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" />
-
- <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:
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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:
- <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" />
-
- <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" />
-
- <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:
- <Button x:Name="BtnPlusMinus" Content="±" HorizontalAlignment="Left" Margin="752,568,0,0" VerticalAlignment="Top" Width="115" Background="Black" Height="76" BorderBrush="Red" FontSize="36" />
-
- <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"/>
-
- <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:
- <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" />
-
- <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" />
-
- <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"/>
-
- <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" />
-
- <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:
- <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:
Now inside the code behind you need to write the following code:
- public sealed partial class MainPage : Page
- {
- public bool status;
- public bool Dot;
- public double PreviousValue = 0.0;
- public char Operation;
- public bool FirstTime = true;
- public MainPage()
- {
- this.InitializeComponent();
- TxtBox.IsReadOnly = true;
- TxtBox.Text = "0";
- }
Now write the following code for the number buttons click event.
- #region Number Buttons
- private void BtnZero_Click(object sender, RoutedEventArgs e)
- {
- if (status == true || BtnZero.Content.ToString() != "0")
- {
- TxtBox.Text += BtnZero.Content;
- }
- else
- {
- TxtBox.Text = BtnZero.Content.ToString();
- }
- }
-
- private void Btn1_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn1.Content;
- }
- else
- {
- TxtBox.Text = Btn1.Content.ToString();
- status = true;
- }
- }
-
- private void Btn2_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn2.Content;
- }
- else
- {
- TxtBox.Text = Btn2.Content.ToString();
- status = true;
- }
- }
-
- private void Btn3_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn3.Content;
- }
- else
- {
- TxtBox.Text = Btn3.Content.ToString();
- status = true;
- }
- }
-
- private void Btn4_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn4.Content;
- }
- else
- {
- TxtBox.Text = Btn4.Content.ToString();
- status = true;
- }
- }
-
- private void Btn5_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn5.Content;
- }
- else
- {
- TxtBox.Text = Btn5.Content.ToString();
- status = true;
- }
- }
-
- private void Btn6_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn6.Content;
- }
- else
- {
- TxtBox.Text = Btn6.Content.ToString();
- status = true;
- }
- }
-
- private void Btn7_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn7.Content;
- }
- else
- {
- TxtBox.Text = Btn7.Content.ToString();
- status = true;
- }
- }
-
- private void Btn8_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn8.Content;
- }
- else
- {
- TxtBox.Text = Btn8.Content.ToString();
- status = true;
- }
- }
-
- private void Btn9_Click(object sender, RoutedEventArgs e)
- {
- if (status == true)
- {
- TxtBox.Text += Btn9.Content;
- }
- else
- {
- TxtBox.Text = Btn9.Content.ToString();
- status = true;
- }
- }
- #endregion Number Buttons
For the Dot button's click event you can write the following code:
-
- private void BtnDot_Click(object sender, RoutedEventArgs e)
- {
- if (Dot == false)
- {
- TxtBox.Text += BtnDot.Content.ToString();
- Dot = true;
- status = true;
- }
- }
For the arithmetic operations write the following code:
- #region Arithmatic Operation
- private void BtnAdd_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = '+';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = '+';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
-
- private void BtnSub_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = '-';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = '-';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
-
- private void BtnMul_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = '*';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = '*';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
-
- private void BtnDiv_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = '/';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = '/';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
The following is the code for the equals button's click event:
-
- private void BtnEqual_Click(object sender, RoutedEventArgs e)
- {
- if (Operation != '\0')
- {
- OpreationMethod();
- TxtBox.Text = PreviousValue.ToString();
- PreviousValue = 0.0;
- }
- status = false;
- FirstTime = true;
- Operation = '\0';
- }
The following is the code for the Click event of the calculator's OFF, AC and CE buttons:
-
- private void BtnOff_Click(object sender, RoutedEventArgs e)
- {
- BtnZero.IsEnabled = false;
- Btn1.IsEnabled = false;
- Btn2.IsEnabled = false;
- Btn3.IsEnabled = false;
- Btn4.IsEnabled = false;
- Btn5.IsEnabled = false;
- Btn6.IsEnabled = false;
- Btn7.IsEnabled = false;
- Btn8.IsEnabled = false;
- Btn9.IsEnabled = false;
- Btn1byX.IsEnabled = false;
- BtnAdd.IsEnabled = false;
- BtnBackSpace.IsEnabled = false; ;
- BtnCE.IsEnabled = false;
- BtnCos.IsEnabled = false; ;
- BtnCosInv.IsEnabled = false;
- BtnCube.IsEnabled = false;
- BtnDiv.IsEnabled = false;
- BtnDot.IsEnabled = false;
- BtnEqual.IsEnabled = false;
- BtnFact.IsEnabled = false;
- BtnLN.IsEnabled = false;
- BtnLog.IsEnabled = false;
- BtnMul.IsEnabled = false;
- BtnNCR.IsEnabled = false; ;
- BtnNPR.IsEnabled = false;
- BtnPercent.IsEnabled = false;
- BtnPI.IsEnabled = false;
- BtnPlusMinus.IsEnabled = false;
- BtnPow.IsEnabled = false;
- BtnRoot.IsEnabled = false;
- BtnSin.IsEnabled = false;
- BtnSinInv.IsEnabled = false;
- BtnSquare.IsEnabled = false;
- BtnTan.IsEnabled = false;
- BtnTanInv.IsEnabled = false;
- BtnSub.IsEnabled = false;
-
- PreviousValue = 0.0;
- TxtBox.Text = string.Empty;
- Dot = false;
- FirstTime = true;
- }
-
- private void BtnAc_Click(object sender, RoutedEventArgs e)
- {
- BtnZero.IsEnabled = true;
- Btn1.IsEnabled = true;
- Btn2.IsEnabled = true;
- Btn3.IsEnabled = true;
- Btn4.IsEnabled = true;
- Btn5.IsEnabled = true;
- Btn6.IsEnabled = true;
- Btn7.IsEnabled = true;
- Btn8.IsEnabled = true;
- Btn9.IsEnabled = true;
- Btn1byX.IsEnabled = true;
- BtnAdd.IsEnabled = true;
- BtnBackSpace.IsEnabled = true;
- BtnCE.IsEnabled = true;
- BtnCos.IsEnabled = true; ;
- BtnCosInv.IsEnabled = true;
- BtnCube.IsEnabled = true;
- BtnDiv.IsEnabled = true;
- BtnDot.IsEnabled = true;
- BtnEqual.IsEnabled = true;
- BtnFact.IsEnabled = true;
- BtnLN.IsEnabled = true;
- BtnLog.IsEnabled = true;
- BtnMul.IsEnabled = true;
- BtnNCR.IsEnabled = true; ;
- BtnNPR.IsEnabled = true;
- BtnPercent.IsEnabled = true;
- BtnPI.IsEnabled = true;
- BtnPlusMinus.IsEnabled = true;
- BtnPow.IsEnabled = true;
- BtnRoot.IsEnabled = true;
- BtnSin.IsEnabled = true;
- BtnSinInv.IsEnabled = true;
- BtnSquare.IsEnabled = true;
- BtnTan.IsEnabled = true;
- BtnTanInv.IsEnabled = true;
- BtnSub.IsEnabled = true;
-
- PreviousValue = 0.0;
- TxtBox.Text = "0";
- Dot = false;
- FirstTime = true;
- }
-
- private void BtnCE_Click(object sender, RoutedEventArgs e)
- {
- TxtBox.Text = "0";
- }
The following is the code for the Click Event of the Clear History button:
-
- private void BtnClearHistory_Click(object sender, RoutedEventArgs e)
- {
- History.Items.Clear();
- }
The following is the code for Backspace button's Click Event:
- private void BtnBackSpace_Click(object sender, RoutedEventArgs e)
- {
- if (TxtBox.Text != "0")
- {
- string str = TxtBox.Text;
- int n = str.Length;
- TxtBox.Text = (str.Substring(0, n - 1));
- }
- if (TxtBox.Text.Length == 0)
- {
- TxtBox.Text = "0";
- status = false;
- }
- }
The following is the code for the Logarithmic (for the Click Event of the Log and ln buttons) function:
-
- private void BtnLog_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'l';
- OpreationMethod();
- }
-
- private void BtnLN_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'n';
- OpreationMethod();
- }
The following is the code for Power function (for the Click Event of the X^2, X^3 and X^Y buttons):
-
- private void BtnPow_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = '^';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = '^';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
-
-
- private void BtnSquare_Click(object sender, RoutedEventArgs e)
- {
- Operation = '2';
- OpreationMethod();
- }
-
-
- private void BtnCube_Click(object sender, RoutedEventArgs e)
- {
- Operation = '3';
- OpreationMethod();
- }
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)
-
- private void BtnSin_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'S';
- OpreationMethod();
- }
-
- private void BtnCos_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'C';
- OpreationMethod();
- }
-
- private void BtnTan_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'T';
- OpreationMethod();
- }
-
- private void BtnSinInv_Click(object sender, RoutedEventArgs e)
- {
- Operation = 's';
- OpreationMethod();
- }
-
- private void BtnCosInv_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'c';
- OpreationMethod();
- }
-
- private void BtnTanInv_Click(object sender, RoutedEventArgs e)
- {
- Operation = 't';
- OpreationMethod();
- }
The following is the code for Factorial, Permutations and Combinations (for the Click Event of the X!, nPr and nCr buttons):
-
- private void BtnFact_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'f';
- OpreationMethod();
- }
-
- private void BtnNCR_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = 'R';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = 'R';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
-
- private void BtnNPR_Click(object sender, RoutedEventArgs e)
- {
- if (!FirstTime)
- {
- OpreationMethod();
- Operation = 'r';
- status = false;
- }
- else
- {
- PreviousValue = double.Parse(TxtBox.Text);
- Operation = 'r';
- status = false;
- FirstTime = false;
- }
- TxtBox.Text = "0";
- }
The following is the code for the Click Event of the PI, +/- and 1/X buttons:
-
- private void BtnPI_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'p';
- OpreationMethod();
- }
-
-
- private void BtnPlusMinus_Click(object sender, RoutedEventArgs e)
- {
- TxtBox.Text = (-1.0 * double.Parse(TxtBox.Text)).ToString();
- }
-
-
- private void Btn1byX_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'o';
- OpreationMethod();
- }
The following is the code for the Click Event of the Square Root Button:
- private void BtnRoot_Click(object sender, RoutedEventArgs e)
- {
- Operation = 'q';
- OpreationMethod();
- }
The following is the code for the Click Event of the Percentage Buttons:
-
- private void BtnPercent_Click(object sender, RoutedEventArgs e)
- {
- TxtBox.Text = (PreviousValue * double.Parse(TxtBox.Text) / 100).ToString();
- }
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.
I have created one factorial function that finds the factorial as given below:
- private int factorial(int x)
- {
- int i = 1;
- for (int s = 1; s <= x; s++)
- {
- i = i * s;
- }
- return i;
- }
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.
- Open Solution Explorer.
- Open the Package.appxmanifest file.
- 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:
- Output in Start Menu.
![Start Menu]()
- Output after running the project.
![running the project]()
- Output of calculator:
![calculator]()