Question: Extra 7-2 Add data validation to the simple calculator In this exercise, youll add data validation to the Simple Calculator form of extra exercise 7-1.
Extra 7-2 Add data validation to the simple calculator In this exercise, youll add data validation to the Simple Calculator form of extra exercise 7-1. 1. Open the SimpleCalculator project in the Extra Exercises\Chapter 07\SimpleCalculator With Data Validation directory. 2. Code methods named IsPresent, IsDecimal, and IsWithinRange that work like the methods described in chapter 7 of the book. 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 properl
heres what I got so far
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 SampleCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e)
{ decimal operand1 = decimal.Parse(txtOperand1.Text); decimal operand2 = decimal.Parse(txtOperand2.Text); string operator1 = txtOperator.Text;
txtResult.Text = String.Format($"{Calculate(operand1, operand2, operator1).ToString("f4")}"); txtOperand1.Focus(); }
private decimal Calculate(decimal operand1, decimal operand2, string operator1)
{ decimal result = 0; switch (operator1) { case "+": result = operand1 + operand2; break;
case "-": result = operand1 - operand2; break;
case "*": result = operand1 * operand2; break;
case "/": result = operand1 / operand2; break; }
return result;
} private void btnExit_Click(object sender, EventArgs e)
{ Application.Exit(); }
private void txtOperand1_TextChanged(object sender, EventArgs e)
{ txtResult.Text = ""; } private void txtOperand2_TextChanged(object sender, EventArgs e)
{ txtResult.Text = ""; }
private void btnExit_Click_1(object sender, EventArgs e) { this.Close(); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
