Walkthrough: Creating a WPF calculator


Step 1: Start your Microsoft Visual Studio. From File menu, create a new project. Project dialog appears. From the project dialog, select WPF application from C# windows application. Rename the default name to My Calculator then click OK.


Step 2: In the solution explorer, rename the Window1.xaml file to Calculator.xaml. Then locate App.xaml in solution explorer and open it in designer mode. In the 4th line, change StartUri property from Window1.xaml to Calculator.xaml. Press F5. A blank window titled Window1 will appears. Your project is ready now.

Step 3: Open Calculator.xaml in design mode. Change its title to MY Calculator. You can change it either in calculator.xaml file or in its property window. If you don’t see the property window, press Ctrl+W then P.

Step 4: Locate in calculator.xaml. delete it and paste the code bellow.
<Window.Resources>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="15" />
            <Setter Property="Background" Value="Ivory" />
            <Setter Property="Foreground" Value="Brown"/>
        Style>
      Window.Resources>
This code will set your buttons font size to 15, background color to Ivory and text color to Brown and it will be your Button’s default property.

Step 5: After paste the code bellow.
<DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File" Name="FileMenu">
                <MenuItem Header="Exit" Click="ExitClick"/>
            MenuItem>
            <MenuItem Header="View">
                <MenuItem Header="Standerd" IsCheckable="True" IsChecked="True"/>
            MenuItem>
            <MenuItem Header="About" Click="AboutClick"/>
            <MenuItem Header="Help" Click="HelpClick"/>
        Menu>
 DockPanel>
This will create menu for your calculator.

Step 6: After paste the following code
<Grid Background="LightGreen">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
            Grid.RowDefinitions>
        Grid>
This will divide your window to 8 columns and 6 rows.

Step 7: After paste the following code.
            <Grid Grid.ColumnSpan="8" Background="Snow" FlowDirection="LeftToRight">
               
                <TextBox Name="txtBox" FontSize="15" HorizontalScrollBarVisibility="Auto" BorderThickness="0" Margin="8,0,0,0">
                   
                TextBox>
                <Label Margin="8,8,8,16" Name="StatusLabel" Visibility="Hidden" Background="Transparent">Label>
            Grid>
           
            <Button Name="Ans" Content="Ans" Grid.Row="1" Margin="0,0,0,18" Click="Ans_Click" ToolTip="Last expression's value.">
                Button>
            <Button Name="MR" Content="MR" Grid.Row="1" Grid.Column="1" Margin="0,0,0,18" Click="MR_Click" ToolTip="No value specified for memory.">
                Button>
            <Button Name="M" Content="M+" Grid.Column="6" Grid.Row="1" Margin="0,0,0,18" Click="M_Click" ToolTip="Click this Button to store active result or screan content in memory. ">
                   Button>
            <Button Name="MC" Content="MC" Grid.Column="7" Grid.Row="1" Margin="0,0,0,18" ToolTip="Click to release the memory." Click="MC_Click">
                Button>
            <Button Content="0" Grid.Row="5" Grid.ColumnSpan="2" Click="dgtClick"/>
            <Button Content="." Grid.Column="2" Grid.Row="5" Click="dgtClick"/>
            <Button Content="1" Grid.Row="4" Click="dgtClick"/>
            <Button Content="2" Grid.Column="1" Grid.Row="4" Click="dgtClick"/>
            <Button Content="3" Grid.Column="2" Grid.Row="4" Click="dgtClick"/>
            <Button Content="4" Grid.Row="3" Click="dgtClick"/>
            <Button Content="5" Grid.Column="1" Grid.Row="3" Click="dgtClick"/>
            <Button Content="6" Grid.Column="2" Grid.Row="3" Click="dgtClick"/>
            <Button Content="7" Grid.Row="2" Click="dgtClick"/>
            <Button Content="8" Grid.Column="1" Grid.Row="2" Click="dgtClick"/>
            <Button Content="9" Grid.Row="2" Grid.Column="2" Click="dgtClick"/>
            <Button Content="=" Grid.Column="4" Grid.Row="4" Grid.RowSpan="2" Click="EqualClick"/>
            <Button Content="+" Grid.Column="5" Grid.Row="4" Grid.RowSpan="2" Click="dgtClick"/>
            <Button Content="-" Grid.Column="6" Grid.Row="4" Click="dgtClick"/>
            <Button Content="*" Grid.Column="5" Grid.Row="3" Click="dgtClick"/>
            <Button Content="/" Grid.Column="5" Grid.Row="2" Click="dgtClick"/>
            <Button Content="%" Grid.Column="6" Grid.Row="3" Click="dgtClick"/>
            <Button Content="!" Grid.Column="6" Grid.Row="2" Click="dgtClick"/>
            <Button Content="(" Grid.Column="4" Grid.Row="2" Click="dgtClick"/>
            <Button Content=")" Grid.Column="4" Grid.Row="3" Click="dgtClick"/>
            <Button Content="Sqrt" Grid.Column="6" Grid.Row="5" Click="SqrtClick"/>
            <Button Content="Del" Grid.Column="7" Grid.Row="2" Click="DelClick" ToolTip="Deletes a character from the screan right to left." />
            <Button Content="Reset" Grid.Column="7" Grid.Row="3" Click="ClearClick" ToolTip="Resets Calculator to initial state." />
            <Button Content="Exit" Grid.Column="7" Grid.Row="4" Grid.RowSpan="2" Click="ExitClick" Tag="Closes the Calculator." />
          
This will make your Buttons, textboxes, labels that all you needed. Now the entire code will be as bellow for Calculator.xaml
<Window x:Class="My_Calculator.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="My Calculator" Height="350" Width="430">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="15" />
            <Setter Property="Background" Value="Ivory" />
            <Setter Property="Foreground" Value="Brown"/>
        Style>
    Window.Resources>
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File" Name="FileMenu">
                <MenuItem Header="Exit" Click="ExitClick"/>
            MenuItem>
            <MenuItem Header="View">
                <MenuItem Header="Standerd" IsCheckable="True" IsChecked="True"/>
            MenuItem>
            <MenuItem Header="About" Click="AboutClick"/>
            <MenuItem Header="Help" Click="HelpClick"/>
        Menu>
        <Grid Background="LightGreen">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition>ColumnDefinition>
                <ColumnDefinition>ColumnDefinition>
                <ColumnDefinition>ColumnDefinition>
                <ColumnDefinition>ColumnDefinition>
            Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
                <RowDefinition>RowDefinition>
            Grid.RowDefinitions>
            <Grid Grid.ColumnSpan="8" Background="Snow" FlowDirection="LeftToRight">
               
                <TextBox Name="txtBox" FontSize="15" HorizontalScrollBarVisibility="Auto" BorderThickness="0" Margin="8,0,0,0">

                TextBox>
                <Label Margin="8,8,8,16" Name="StatusLabel" Visibility="Hidden" Background="Transparent">Label>
            Grid>

            <Button Name="Ans" Content="Ans" Grid.Row="1" Margin="0,0,0,18" Click="Ans_Click" ToolTip="Last expression's value.">
            Button>
            <Button Name="MR" Content="MR" Grid.Row="1" Grid.Column="1" Margin="0,0,0,18" Click="MR_Click" ToolTip="No value specified for memory.">
            Button>
            <Button Name="M" Content="M+" Grid.Column="6" Grid.Row="1" Margin="0,0,0,18" Click="M_Click" ToolTip="Click this Button to store active result or screan content in memory. ">
            Button>
            <Button Name="MC" Content="MC" Grid.Column="7" Grid.Row="1" Margin="0,0,0,18" ToolTip="Click to release the memory." Click="MC_Click">
            Button>
            <Button Content="0" Grid.Row="5" Grid.ColumnSpan="2" Click="dgtClick"/>
            <Button Content="." Grid.Column="2" Grid.Row="5" Click="dgtClick"/>
            <Button Content="1" Grid.Row="4" Click="dgtClick"/>
            <Button Content="2" Grid.Column="1" Grid.Row="4" Click="dgtClick"/>
            <Button Content="3" Grid.Column="2" Grid.Row="4" Click="dgtClick"/>
            <Button Content="4" Grid.Row="3" Click="dgtClick"/>
            <Button Content="5" Grid.Column="1" Grid.Row="3" Click="dgtClick"/>
            <Button Content="6" Grid.Column="2" Grid.Row="3" Click="dgtClick"/>
            <Button Content="7" Grid.Row="2" Click="dgtClick"/>
            <Button Content="8" Grid.Column="1" Grid.Row="2" Click="dgtClick"/>
            <Button Content="9" Grid.Row="2" Grid.Column="2" Click="dgtClick"/>
            <Button Content="=" Grid.Column="4" Grid.Row="4" Grid.RowSpan="2" Click="EqualClick"/>
            <Button Content="+" Grid.Column="5" Grid.Row="4" Grid.RowSpan="2" Click="dgtClick"/>
            <Button Content="-" Grid.Column="6" Grid.Row="4" Click="dgtClick"/>
            <Button Content="*" Grid.Column="5" Grid.Row="3" Click="dgtClick"/>
            <Button Content="/" Grid.Column="5" Grid.Row="2" Click="dgtClick"/>
            <Button Content="%" Grid.Column="6" Grid.Row="3" Click="dgtClick"/>
            <Button Content="!" Grid.Column="6" Grid.Row="2" Click="dgtClick"/>
            <Button Content="(" Grid.Column="4" Grid.Row="2" Click="dgtClick"/>
            <Button Content=")" Grid.Column="4" Grid.Row="3" Click="dgtClick"/>
            <Button Content="Sqrt" Grid.Column="6" Grid.Row="5" Click="SqrtClick"/>
            <Button Content="Del" Grid.Column="7" Grid.Row="2" Click="DelClick" ToolTip="Deletes a character from the screan right to left." />
            <Button Content="Reset" Grid.Column="7" Grid.Row="3" Click="ClearClick" ToolTip="Resets Calculator to initial state." />
            <Button Content="Exit" Grid.Column="7" Grid.Row="4" Grid.RowSpan="2" Click="ExitClick" Tag="Closes the Calculator." />

        Grid>
    DockPanel>
Window>

Don’t forget to save all.
Step 8: Now from the File Menu, click Add new project. In the project template, Select ClassLibrary, give name EvalLib then click OK. Rename The class1.cs to MathEval.cs.
Step 9: Now your solution explorer contains 2 projects, My Calculator and EvalLib projects. In EvalLib project menu, click Add reference. From .Net click System.Windows.Form and then click OK.

Step 10: Replace your MathEval.cs to following code.
using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Windows;
namespace EvalLib
{
    public class MathEval
    {
        private string sb=null;
        private bool canUnaryMinus = true;
        public double TextToValue(string text)
        {
            int l = text.Length;
            polish(text);
            double p = Eval_polish();
            return p;
        }
        private void polish(string text)
        {
            char[] stact=new char[30];///stack used for operator storing.
            int top = 1,i=0;
            stact[0] = '('///initially push ( to stack
            text = text+")";
            while (top != 0)
            {
                string a=null; ///a variable for storing operands
                if ((text[i] > 47 &&text[i] < 58)||text[i]=='.')
                {
                    try
                    {
                        while ((text[i] > 47 && text[i] < 58) || text[i] == '.')
                        {
                            a += text[i].ToString();

                            i++;
                        }
                        sb += (a.ToString());
                        sb += ","; ///sentinell to seperate operands
                        canUnaryMinus = false;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
                else if (text[i]=='+'||text[i]=='-'||text[i]=='*'||text[i]=='/'||text[i]=='%'||text[i]=='!')
                {
                    int p = Precedance(stact[top-1]), q = Precedance(text[i]);
                    while (p >= q)

                    {
                        top--;
                        sb += (stact[top].ToString());
                        sb += ",";
                       
                        p = Precedance(stact[top-1]);
                    }
                    if (q == 5)
                        stact[top] = 'u';/// u represents unary minus.
                    else
                        stact[top] = text[i];
                    i++;
                    top++;
                    canUnaryMinus = true;
                }
                else if (text[i] == '(')
                {
                    stact[top] = text[i];
                    top++;
                    i++;
                }
                else if (text[i] == ')')
                {
                    while (stact[top-1] != '(')
                    {
                        top--;
                        sb += (stact[top].ToString());
                        sb += ",";
                    }
                    top--;
                    i++;
                }
                else return;
            }
          
        }
        private int Precedance(char a)///returns operator precedance
        {
            if (canUnaryMinus && a == '-')
            {
                canUnaryMinus = false;
                return 5;
            }
            int precedenceNumber = 0;
            switch (a)
            {
                case '+':
                case '-':
                    precedenceNumber=1;
                    break;
                case '*':
                case '/':
                    precedenceNumber= 2;
                    break;
                case '%':
                    precedenceNumber= 3;
                    break;
                case '!':
                    precedenceNumber= 4;
                    break;
                case 'u':
                    precedenceNumber = 5;
                    break;
            }
            return precedenceNumber;
        }
        private double Eval_polish()
        {
            double[] stact = new double[32];
            int top = 0,length=sb.Length-1,i=0;
            try
            {
                while (i < length)
                {
                    string s = null;
                again:
                    if (sb[i] == ',')
                    {
                        i++;
                    }
                    else if (sb[i] > 47 && sb[i] < 58||sb[i]=='.')
                    {
                        s += sb[i].ToString();
                        i++;
                        goto again;
                    }
                    if (s != null)
                    {
                        stact[top] = double.Parse(s);
                        top++;
                        s = null;
                    }
                    if (sb[i] == '+' || sb[i] == '-' || sb[i] == '*' || sb[i] == '/' || sb[i] == '%')
                    {

                        double a, b;
                        top--;
                        a = stact[top];
                        top--;
                        b = stact[top];

                        switch (sb[i])
                        {
                            case '+':
                                a += b;
                                break;
                            case '-':
                                a = b - a;
                                break;
                            case '*':
                                a = b * a;
                                break;
                            case '/':
                                a = b / a;
                                break;
                            case '%':
                                a = b % a;
                                break;
                        }
                        stact[top] = a;
                        top++;
                        i++;
                    }
                    else if (sb[i] == 'u' || sb[i] == '!')
                    {
                        double a, b = 1;
                        top--;
                        a = stact[top];
                        if (sb[i] == 'u')
                        {
                            a = -a;
                        }
                        else
                        {
                            if (a > 170)
                            {
                                System.Windows.Forms.MessageBox.Show("Too Big for factorialization.\nIt can't be more then 170.", "Error!");
                                return 0;
                            }
                            for (double mul = 1; mul <= a; mul++)
                            {
                                b *= mul;
                            }
                            a = b;
                        }
                        stact[top] = a;
                        top++;
                        i++;
                    }

                }

                return stact[top - 1];
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error");
                return 0;
            }
        }

    }
}
Hare TextToValue is the public function and it will be accessible from outside the class. It takes a string argument which is an infix notation expression. Then convert it to postfix using private Polish function. The Polish function usage private Precedence function  to determine the precedence role for operator. Finally TextToValue uses Eval_polish function which determines the value of newly created postfix expression.

Step 11: Now right click on the EvalLib project. Click build. This will create a dll file. Go to My Calculator project. From reference menu click add reference. Click on project Tab, click EvalLib. On top of your Calculator.xaml.cs add the code bellow.
using EvalLib;
Now your Calculator is ready to use EvalLib library.

Step 12: Now go to your Calculator.xaml.cs file. Now right after public Window1() function’s last braces, add the code bellow,
          string Answer = null, Memory = null;
      bool isReady = false;
the string variable will store memory for the calculator and Boolean variable will decide whether your calculator is ready or not.

Step 13: Go to designer view of Calculator. Double click on any digit button. Copy the code bellow where the cursor blinks.
if (!isReady)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = ((Button)sender).Content.ToString();
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += ((Button)sender).Content;
            }

Step 14: Double click on Ans, MR, M+, MC, Sqrt, Del, Reset, Exit, =, About, Help and copy the code bellow as described above.
For Ans: if (!isReady)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = Answer;
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += Answer;
            }

For MR: if (!isReady&&Memory!=null)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = Memory;
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += Memory;
            }

For M+: if (isReady)
            {
                Memory = txtBox.Text;
            }
            else
            {
                Memory = Answer;
            }
            MR.ToolTip = Memory;

For MC: Memory = null;
         MR.ToolTip = "No value specified for memory.";

For Sqrt: try
            {
                double b = double.Parse(txtBox.Text);
                b = Math.Sqrt(b);
                txtBox.Text = b.ToString();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Error");
            }

For Del:  try
            {
                if (!isReady)
                {
                    txtBox.VerticalAlignment = VerticalAlignment.Top;
                    StatusLabel.Visibility = Visibility.Hidden;
                    txtBox.Text = Answer;
                    isReady = true;
                }
                else
                {
                    txtBox.Text = txtBox.Text.Substring(0, txtBox.Text.Length - 1);
                }
            }
            catch (Exception)
            {
                isReady = false;
                /// Nothing should be done.
                /// Because this will arive
                /// when No value prevails in the screan.
            }

For Reset: txtBox.VerticalAlignment = VerticalAlignment.Top;
            StatusLabel.Visibility = Visibility.Hidden;
            isReady = false;
            Memory = null;
            txtBox.Text = null;
            MR.ToolTip = "No value specified for memory.";
            this.Height = 359;
            this.Width = 419;
            Answer = null;

For Exit: this.Close();

For =:  EvalLib.MathEval evaluate = new MathEval();
            try
            {
                for (int i = 0; i < txtBox.Text.Length; i++)
                {
                    if (txtBox.Text[i] == '!' || ((txtBox.Text[i] >= 40 && txtBox.Text[i] <= 57) && txtBox.Text[i] != 44) || txtBox.Text[i] == 37)//ASCII
                    {
                        continue;
                    }
                    throw new KeyNotFoundException();
                }
                double result = evaluate.TextToValue(txtBox.Text);
                StatusLabel.Content = txtBox.Text;
                txtBox.VerticalAlignment = VerticalAlignment.Bottom;
                txtBox.Text = "=" + result.ToString();
                StatusLabel.Visibility = Visibility.Visible;
                isReady = false;
                Answer = result.ToString();

            }
            catch (KeyNotFoundException)
            {
                MessageBox.Show("Your input contains a character\nthat can't be calculated.", "Error");
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Error");
            }

For About: string about = "Write down your own Name, company, and what you want to tell the user about you.";
            MessageBox.Show(about, "About My Calculator", MessageBoxButton.OK, MessageBoxImage.Information);
   
For Help: string help =
                "        Just type any kind of complex or simple expression,\n" +
                "the calculator have flexibility to produce accurate result.\n\n" +

                "        You can use memory for store either a result or a \n" +
                "complete expression. If there are any expression prevails \n" +
                "in the screan, this will be stored if you press 'M+'. By \n" +
                "pressing 'MR' you can add the memory content at any\n" +
                "time to screan.'MC' will clear the memory. 'Ans' is an\n" +
                "automatic memory. Every time you have a value for an\n" +
                "expression, the resulted value will be stored in 'Ans'. You\n" +
                "can only use it same as 'MR' but you can't assign anything\n" +
                "to it as it is an automatic memory.\n\n" +


                "         Reset will restore the calculator to its initial value.\n" +
                "Remember, ALL MEMORY WILL BE LOST IF YOU CLICK 'Reset'\n" +
                "You can use Del to clear the memory character by character.\n\n" +

                "         Have fun using this calculator.";
            MessageBox.Show(help, "Help", MessageBoxButton.OK, MessageBoxImage.Information);

Step 15: Now your calculator is ready to use. From Build menu, click Build Solution. Then click Ctrl+F5. Wow! You got a standard calculator made by your own hand. Use it and check all functionality works. For any question, feel free to ask me at mail: asadiceiu@gmail.com or a better way call me at +8801737079402.
The window will look like:
For you all the code is given bellow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using EvalLib;
namespace My_Calculator
{
    ///
    /// Interaction logic for Window1.xaml
    ///
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        string Answer = null, Memory = null;
        bool isReady = false;

        private void dgtClick(object sender, RoutedEventArgs e)
        {
            if (!isReady)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = ((Button)sender).Content.ToString();
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += ((Button)sender).Content;
            }
        }

        private void Ans_Click(object sender, RoutedEventArgs e)
        {
            if (!isReady)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = Answer;
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += Answer;
            }
        }

        private void MR_Click(object sender, RoutedEventArgs e)
        {
            if (!isReady && Memory != null)
            {
                txtBox.VerticalAlignment = VerticalAlignment.Top;
                txtBox.Text = Memory;
                isReady = true;
                StatusLabel.Visibility = Visibility.Hidden;
            }
            else
            {
                txtBox.Text += Memory;
            }
        }

        private void M_Click(object sender, RoutedEventArgs e)
        {
            if (isReady)
            {
                Memory = txtBox.Text;
            }
            else
            {
                Memory = Answer;
            }
            MR.ToolTip = Memory;
        }

        private void MC_Click(object sender, RoutedEventArgs e)
        {
            Memory = null;
            MR.ToolTip = "No value specified for memory.";
        }
  private void SqrtClick(object sender, RoutedEventArgs e)
        {
            try
            {
                double b = double.Parse(txtBox.Text);
                b = Math.Sqrt(b);
                txtBox.Text = b.ToString();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Error");
            }
        }

       

        private void DelClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!isReady)
                {
                    txtBox.VerticalAlignment = VerticalAlignment.Top;
                    StatusLabel.Visibility = Visibility.Hidden;
                    txtBox.Text = Answer;
                    isReady = true;
                }
                else
                {
                    txtBox.Text = txtBox.Text.Substring(0, txtBox.Text.Length - 1);
                }
            }
            catch (Exception)
            {
                isReady = false;
                /// Nothing should be done.
                /// Because this will arive
                /// when No value prevails in the screan.
            }
        }

        private void ClearClick(object sender, RoutedEventArgs e)
        {
            txtBox.VerticalAlignment = VerticalAlignment.Top;
            StatusLabel.Visibility = Visibility.Hidden;
            isReady = false;
            Memory = null;
            txtBox.Text = null;
            MR.ToolTip = "No value specified for memory.";
            this.Height = 359;
            this.Width = 419;
            Answer = null;
        }

        private void ExitClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void EqualClick(object sender, RoutedEventArgs e)
        {
            EvalLib.MathEval evaluate = new MathEval();
            try
            {
                for (int i = 0; i < txtBox.Text.Length; i++)
                {
                    if (txtBox.Text[i] == '!' || ((txtBox.Text[i] >= 40 && txtBox.Text[i] <= 57) && txtBox.Text[i] != 44) || txtBox.Text[i] == 37)//ASCII
                    {
                        continue;
                    }
                    throw new KeyNotFoundException();
                }
                double result = evaluate.TextToValue(txtBox.Text);
                StatusLabel.Content = txtBox.Text;
                txtBox.VerticalAlignment = VerticalAlignment.Bottom;
                txtBox.Text = "=" + result.ToString();
                StatusLabel.Visibility = Visibility.Visible;
                isReady = false;
                Answer = result.ToString();

            }
            catch (KeyNotFoundException)
            {
                MessageBox.Show("Your input contains a character\nthat can't be calculated.", "Error");
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Error");
            }
        }

        private void AboutClick(object sender, RoutedEventArgs e)
        {
            string about = " Write down your own Name, company, and what you want to tell the user about you.";
MessageBox.Show(about, "About My Calculator", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void HelpClick(object sender, RoutedEventArgs e)
        {
            string help =
                "        Just type any kind of complex or simple expression,\n" +
                "the calculator have flexibility to produce accurate result.\n\n" +

                "        You can use memory for store either a result or a \n" +
                "complete expression. If there are any expression prevails \n" +
                "in the screan, this will be stored if you press 'M+'. By \n" +
                "pressing 'MR' you can add the memory content at any\n" +
                "time to screan.'MC' will clear the memory. 'Ans' is an\n" +
                "automatic memory. Every time you have a value for an\n" +
                "expression, the resulted value will be stored in 'Ans'. You\n" +
                "can only use it same as 'MR' but you can't assign anything\n" +
                "to it as it is an automatic memory.\n\n" +

                "         Reset will restore the calculator to its initial value.\n" +
                "Remember, ALL MEMORY WILL BE LOST IF YOU CLICK 'Reset'\n" +
                "You can use Del to clear the memory character by character.\n\n" +

                "         Have fun using this calculator.";
            MessageBox.Show(help, "Help", MessageBoxButton.OK, MessageBoxImage.Information);
        }
       
    }
}