示例#1
0
        /// <summary>
        /// Click Event for Operational Buttons
        /// </summary>
        /// <param name="sender">Operational Buttons</param>
        /// <param name="e">Event Argument</param>
        private void OperatorButton_Click(object sender, EventArgs e)
        {
            // Identifying the source button
            Button source = (Button)sender;

            switch (source.Text)
            {
            case "+":
                // Creating delegate for Add Operation
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = string.Empty;
                executeOperation    = new ExecuteOperation(Add);
                break;

            case "-":

                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = string.Empty;
                // Creating delegate for Subtract Operation
                executeOperation = new ExecuteOperation(Subtract);
                break;

            case "*":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = string.Empty;
                // Creating delegate for Multiply Operation
                executeOperation = new ExecuteOperation(Multiply);
                break;

            case "/":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = string.Empty;
                // Creating delegate for Divide Operation
                executeOperation = new ExecuteOperation(Divide);
                break;

            case "=":
            default:
                if (executeOperation != null)
                {
                    double nextNumber = Convert.ToDouble(textBox_Result.Text);
                    // Exception handling
                    try
                    {
                        previousNumber = executeOperation.Invoke(previousNumber, nextNumber);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "Error in Calculator");
                    }
                    finally
                    {
                        textBox_Result.Text = previousNumber.ToString();
                        previousNumber      = 0;
                    }
                }
                break;
            }
        }
示例#2
0
        private void OperatorButton_Click(object sender, EventArgs e)
        {
            Button source = (Button)sender;

            isDot = false;
            switch (source.Text)
            {
            case "+":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = "0";
                executeOperation    = new ExecuteOperation(Add);
                break;

            case "-":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = "0";
                executeOperation    = new ExecuteOperation(Substract);
                break;

            case "*":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = "0";
                executeOperation    = new ExecuteOperation(Multiply);
                break;

            case "/":
                previousNumber      = Convert.ToDouble(textBox_Result.Text);
                textBox_Result.Text = "0";
                executeOperation    = new ExecuteOperation(Devide);
                break;

            case "=":
            default:
                if (executeOperation != null)
                {
                    double nextNumber = Convert.ToDouble(textBox_Result.Text);
                    previousNumber      = executeOperation.Invoke(previousNumber, nextNumber);
                    textBox_Result.Text = previousNumber.ToString();
                    previousNumber      = 0;
                }
                break;
            }
        }