Question
This is c# add validation code and/or exception handling to EACH button Validate that the user actually does enter something on each of the two
This is c#
add validation code and/or exception handling to EACH button
Validate that the user actually does enter something on each of the two text boxes
Validate that Modulus operation is not being done with negative numbers.
If either number is negative in the modulus operation, then a validation check fails and an error message needs to be displayed
Handle any exception created during the conversion of the textbox contents to a number
Handle any exception created by dividing by zero OR prevent it by validation
Handle any "unexpected" exceptions in the entire application
This will require lots of Try/Catch combinations
Display all messages to the user about invalid numbers or exceptions in the label, do not use a MessageBox
Each error or validation check failure needs a message associated with it. Do not do the process where the user fixes one error to just get another because you did not display all the error messages at once
Existing code:
namespace Module6MethodsProjectDL { public partial class frmActors : Form { // Public Contsants to use const byte ADD = 0; const byte SUBTRACT = 1; const byte MULTIPLY = 2; const byte DIVIDE = 3; const byte MODULUS = 4;
public frmActors() { InitializeComponent(); }
private void btnModulo_Click(object sender, EventArgs e) { decimal dLeft = 0.0m; decimal dRight = 0.0m; decimal dAnswer = 0.0m; string szLeft = ""; string szRight = ""; string szAnswer = ""; string szEquation = "";
szLeft = txtLeftOperand.Text; szRight = txtRightOperand.Text;
dLeft = Convert.ToDecimal(szLeft); dRight = Convert.ToDecimal(szRight);
dAnswer = dLeft % dRight;
szAnswer = dAnswer.ToString();
szEquation = szLeft + " % " + szRight + " = " + szAnswer;
lblAnswer.Text = ""; lblAnswer.Text = szEquation;
}
private void btnDivide_Click(object sender, EventArgs e) { decimal dLeft = 0.0m; decimal dRight = 0.0m; decimal dAnswer = 0.0m; string szLeft = ""; string szRight = ""; string szAnswer = ""; string szEquation = "";
szLeft = txtLeftOperand.Text; szRight = txtRightOperand.Text;
dLeft = Convert.ToDecimal(szLeft); dRight = Convert.ToDecimal(szRight);
dAnswer = dLeft / dRight;
szAnswer = dAnswer.ToString();
szEquation = szLeft + " / " + szRight + " = " + szAnswer;
lblAnswer.Text = ""; lblAnswer.Text = szEquation;
}
private void btnMultiply_Click(object sender, EventArgs e) { decimal dLeft = 0.0m; decimal dRight = 0.0m; decimal dAnswer = 0.0m; string szLeft = ""; string szRight = ""; string szAnswer = ""; string szEquation = "";
szLeft = txtLeftOperand.Text; szRight = txtRightOperand.Text;
dLeft = Convert.ToDecimal(szLeft); dRight = Convert.ToDecimal(szRight);
dAnswer = dLeft * dRight;
szAnswer = dAnswer.ToString();
szEquation = szLeft + " * " + szRight + " = " + szAnswer;
lblAnswer.Text = ""; lblAnswer.Text = szEquation;
}
private void btnMinus_Click(object sender, EventArgs e) { decimal dLeft = 0.0m; decimal dRight = 0.0m; decimal dAnswer = 0.0m; string szLeft = ""; string szRight = ""; string szAnswer = ""; string szEquation = "";
szLeft = txtLeftOperand.Text; szRight = txtRightOperand.Text;
dLeft = Convert.ToDecimal(szLeft); dRight = Convert.ToDecimal(szRight);
dAnswer = dLeft - dRight;
szAnswer = dAnswer.ToString();
szEquation = szLeft + " - " + szRight + " = " + szAnswer;
lblAnswer.Text = ""; lblAnswer.Text = szEquation;
}
private void btnPlus_Click(object sender, EventArgs e) { decimal dLeft = 0.0m; decimal dRight = 0.0m; decimal dAnswer = 0.0m; string szLeft = ""; string szRight = ""; string szAnswer = ""; string szEquation = "";
szLeft = txtLeftOperand.Text; szRight = txtRightOperand.Text;
dLeft = Convert.ToDecimal(szLeft); dRight = Convert.ToDecimal(szRight);
dAnswer = dLeft + dRight;
szAnswer = dAnswer.ToString();
szEquation = szLeft + " + " + szRight + " = " + szAnswer;
lblAnswer.Text = ""; lblAnswer.Text = szEquation;
} } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started