Question: I am having a difficult time with the code below and adding data validation. This is what the assignment calls for. 1. Code methods named

I am having a difficult time with the code below and adding data validation. This is what the assignment calls for.

1. Code methods named IsPresent, IsDecimal, and IsWithinRange that work like the methods described in chapter 7 of the book (Murach C#2015).

2. Code a method named IsOperator that checks that the text box thats passed to it contains a value of +, -, *, or /. A sample of the error message for this method can be found attached to this project.

3. 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. The attached form shows entries that can be used to test this.

4. 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.

I am having a difficult time with the code below and addingdata validation. This is what the assignment calls for. 1. Code methods

This what I have competed so far, what may be incorrect?

using System.Collections.Generic;

using System;

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)

{ try {

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(); txtOperand1.Focus();

}

// Handles All Exceptions

catch (Exception ex)

{

MessageBox.Show(ex.ToString(), "Error Occured");

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(); 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;

}

public bool IsValidData()

{

return IsPresent(txtOperand1, "Operand1") &&

IsDecimal(txtOperand1, "Operand1") &&

IsWithinRange(txtOperand1, "Operand1", 0, 1000000) &&

IsPresent(txtOperand2, "Operand2") &&

IsDecimal(txtOperand2, "Operand2") &&

IsWithinRange(txtOperand2, "Operand2", 0, 1000000) &&

IsOperator(txtOperand1, "Operator1");

}

public bool IsPresent(TextBox textBox, string name)

{

if (textBox.Text == "")

{

MessageBox.Show(name + " is a required field.", "Entry Error");

textBox.Focus();

return false;

}

return true;

}

public bool IsDecimal(TextBox textBox, string name)

{ decimal number = 0m; if (Decimal.TryParse(textBox.Text, out number)) {

Convert.ToDecimal(textBox.Text);

return true;

}

else {

MessageBox.Show(name + " must be a decimal value.", "Entry Error");

textBox.Focus(); return false;

}

}

public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)

{

decimal number = Convert.ToDecimal(textBox.Text);

//If within range

if (number max)

{ MessageBox.Show(name + " must be between " + min.ToString() + " and " + max.ToString() + ".", "Entry Error");

textBox.Focus();

return false;

}

return true;

}

public bool IsOperator(TextBox textBox, string name)

{

try

{

Convert.ToString(textBox.Text);

return true;

}

catch (Exception)

{

MessageBox.Show(name + "Operator is not valid.", "Entry Error");

textBox.Focus();

return false;

}

}

private void btnExit_Click(object sender, System.EventArgs e)

{

//Close

this.Close();

}

private void ClearResult(object sender, System.EventArgs e)

{

this.txtResult.Text = "";

}

private void label5_Click(object sender, EventArgs e) {

} }

}

Simple Calculator 1 Operand 1: 256 Operator: Operand 2:0 Result: Calculate Exit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!