Question: In this exercise, youll add data validation to the Simple Calculator form down below. 1. Open the SimpleCalculator project in the Extra ExercisesChapter 07SimpleCalculator With
In this exercise, youll add data validation to the Simple Calculator form down below.


1. Open the SimpleCalculator project in the Extra Exercises\Chapter 07\SimpleCalculator With Data Validation directory.
2. Code methods named IsPresent, IsDecimal, and IsWithinRange.
3. Code a method named IsOperator that checks that the text box thats passed to it contains a value of +, -, *, or /.
4. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.
5. Delete all of the catch blocks from the try-catch statement in the btnCalculate_Click event handler except for the one that catches any exception. Then, add code to this event handler that performs the calculation and displays the result only if the values of the text boxes are valid.
6. Test the application to be sure that all the data is validated properly.
SimpleCalculator code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace SimpleCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, System.EventArgs e) { decimal operand1 = Convert.ToDecimal(txtOperand1.Text); string operator1 = txtOperator.Text; decimal operand2 = Convert.ToDecimal(txtOperand2.Text); decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4); this.txtResult.Text = result.ToString(); this.txtResult.Text = result.ToString();
txtOperand1.Focus(); }
private decimal Calculate(decimal operand1, string operator1, decimal operand2) { decimal result = 0; if (operator1 == "+") result = operand1 + operand2; else if (operator1 == "-") result = operand1 - operand2; else if (operator1 == "*") result = operand1 * operand2; else if (operator1 == "/") result = operand1 / operand2; return result; }
private void ClearResult(object sender, System.EventArgs e) { this.txtResult.Text = ""; }
private void btnExit_Click(object sender, EventArgs e) { this.Close(); } } }
Simple Calculator l - Operand 1: Operator: Operand 2: Result 256 Calculate Exit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
