I\'m having issues creating the event handlers. 1. Copy the Lab 6 project from i
ID: 3593235 • Letter: I
Question
I'm having issues creating the event handlers.
1. Copy the Lab 6 project from its directory to a new directory for lab 7.
2. Open the Project from the Chapter 7 directory
3. Change the name of the Lab6.cs form to Lab7.cs
4. Be sure to add comments in your code.
5. Create an event handler that clears the Result text box if the user changes the text in any of the other text boxes. A wiring event will need to be created for all input textboxes.
6. Add a try-catch statement in the btnCalculate_Click event handler that will catch any exceptions that occur when the statements in that event handler are executed. If an exception occurs, display a dialog box with the error message, the type of error, and a stack trace. Test the application by entering a nonnumeric value for one of the values.
7. Add three additional catch blocks to the try-catch statement that will catch a FormatException, an OverflowException, and a DivideByZeroException. These catch blocks should display a dialog box with an appropriate error message.
8. Test the application again by entering a nonnumeric value for one of the values. Then test by entering 0 for the second value. See images below.
9. Code methods named IsPresent, IsDecimal, and IsWithinRange (min and max range) that work like the methods described in chapter 7 of the book.
10. Code a method named IsOperator that checks that the text box that’s passed to it contains a value of +, -, *, or /.
11. Code a method named IsValidData based off steps 8 and 9 that checks that the Value 1 and Value 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.
12. Comment out 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. Example: If IsValidData()
13. Test the application to be sure that all the data is validated properly.
Heres the code I currently have:
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 Lab6f
{
public partial class Lab6 : Form
{
public Lab6()
{
InitializeComponent();
}
private void Lab6_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal value1 = Convert.ToDecimal(this.txtValue1.Text); //convert text to decimal
decimal value2 = Convert.ToDecimal(this.txtValue2.Text); //convert text to decimal
string operator1 = Convert.ToString(this.txtOperator.Text); //convert operator input to string
decimal totalResult = this.GetResult(value1, value2, operator1); //sending values to method and calling it
txtResult.Text = "" + totalResult; //writing into result textbox
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //closing the program
}
private decimal GetResult(decimal value1, decimal value2, string operator1) //creating the method
{
decimal result = 0;
if (operator1 == "x") //determining the operator to be used
{
result = value1 * value2; //multiplication of the values
}
else if (operator1 == "+")
{
result = value1 + value2; //addition of the values
}
else if (operator1 == "-")
{
result = value1 - value2; //subtraction of the values
}
else if (operator1 == "/")
{
result = value1 / value2; //division of the values
}
return result;
}
}
}
Explanation / Answer
Program Lab lab7.cs
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 Lab6f
{
public partial class Lab7 : Form
{
public Lab7()
{
InitializeComponent();
}
private void Lab6_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
decimal value1 = Convert.ToDecimal(this.txtValue1.Text); //convert text to decimal
decimal value2 = Convert.ToDecimal(this.txtValue2.Text); //convert text to decimal
string operator1 = Convert.ToString(this.txtOperator.Text); //convert operator input to string
decimal totalResult = this.GetResult(value1, value2, operator1); //sending values to method and calling it
txtResult.Text = "" + totalResult; //writing into result textbox
}
}
catch (FormatException ex)
{
MessageBox.Show("Invalid numeric format.Please check all entries.", "Entry Error");
}
catch (DivideByZeroException ex)
{
MessageBox.Show("Divide by Zero error.Please enter a non-zero value for value2", "Entry Error");
}
catch (StackOverflowException ex)
{
MessageBox.Show("Value must be between 0 and 1,000,000", "Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " +
ex.GetType().ToString() + " " +
ex.StackTrace, "Exception");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //closing the program
}
private decimal GetResult(decimal value1, decimal value2, string operator1) //creating the method
{
decimal result = 0;
if (operator1 == "x") //determining the operator to be used
{
result = value1 * value2; //multiplication of the values
}
else if (operator1 == "+")
{
result = value1 + value2; //addition of the values
}
else if (operator1 == "-")
{
result = value1 - value2; //subtraction of the values
}
else if (operator1 == "/")
{
if (value2 != 0)
{
result = value1 / value2; //division of the values
}
else
{
MessageBox.Show("Divide by Zero error.Please enter a non-zero value for value2", "Entry Error");
}
}
return result;
}
// checking whether Operator entered is valid or not
// text change in any of the textboxes result box is made emptied
// double click on the text box to generate event handler
private void txtValue1_TextChanged(object sender, EventArgs e)
{
txtResult.Text = "";
}
// double click on the text box to generate event handler
private void txtValue2_TextChanged(object sender, EventArgs e)
{
txtResult.Text = "";
}
// double click on the text box to generate event handler
private void txtOperator_TextChanged(object sender, EventArgs e)
{
txtResult.Text = "";
}
public bool IsValidData()
{
return
//validate the operand1 text box
IsPresent(txtValue1, "value1") &&
IsDecimal(txtValue1, "value1") &&
IsWithinRange(txtValue1, "value1", 0, 1000000) &&
//validates the operator text box
IsOperator(txtOperator, "Operator") &&
//validates the operand 2 text box
IsPresent(txtValue2, "value2") &&
IsDecimal(txtValue2, "value2") &&
IsWithinRange(txtValue2, "value2", 0, 1000000);
}
//is present
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;
}
//is decimal
public bool IsDecimal(TextBox textBox, string name)
{
decimal number = 0m;
if (Decimal.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textBox.Focus();
return false;
}
}
//is within range
public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + "must be between " + min.ToString()
+ "and " + max.ToString() + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
//is a valid operator
public bool IsOperator(TextBox textBox, string name)
{
if (textBox.Text != "+" && textBox.Text != "-" && textBox.Text != "/" && textBox.Text != "x")
{
MessageBox.Show("Please enter a valid operator in the operator text box.", "Entry Error");
return false;
}
return true;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.