From 08bc2c16e1097f9e4a978af01767ff7def62fc64 Mon Sep 17 00:00:00 2001 From: EggMan20339 <99349302+EggMan20339@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:03:13 -0500 Subject: [PATCH] trying to impliment non / by 0 --- ElementaryMathematics/Form1.Designer.cs | 1 + ElementaryMathematics/Form1.cs | 73 +++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/ElementaryMathematics/Form1.Designer.cs b/ElementaryMathematics/Form1.Designer.cs index ceaabf9..40dfd09 100644 --- a/ElementaryMathematics/Form1.Designer.cs +++ b/ElementaryMathematics/Form1.Designer.cs @@ -211,6 +211,7 @@ this.btnShowCorrectAnswer.TabIndex = 7; this.btnShowCorrectAnswer.Text = "Show Correct"; this.btnShowCorrectAnswer.UseVisualStyleBackColor = true; + this.btnShowCorrectAnswer.Click += new System.EventHandler(this.btnShowCorrectAnswer_Click); // // btnChkAnswer // diff --git a/ElementaryMathematics/Form1.cs b/ElementaryMathematics/Form1.cs index d1875e9..e66bcc6 100644 --- a/ElementaryMathematics/Form1.cs +++ b/ElementaryMathematics/Form1.cs @@ -124,7 +124,7 @@ namespace ElementaryMathematics private ErrorProvider ep1 = new ErrorProvider(); - private decimal Calculate(decimal a, decimal b, decimal c) + private decimal Calculate(decimal a, decimal b) { decimal answer = 0; @@ -147,8 +147,16 @@ namespace ElementaryMathematics hasTried = true; }else if (isDivide) { - answer = a / b; hasTried = true; + if (b != 0) + { + answer = a / b; + } + else + { + throw new Exception(); + } + } else { @@ -158,7 +166,7 @@ namespace ElementaryMathematics } catch (Exception e) { - MessageBox.Show("Whoops!"); + ep1.SetError(btnChkAnswer, "Calculation Error"); } @@ -209,7 +217,7 @@ namespace ElementaryMathematics try { - if (c == Calculate(a,b,c)) + if (c == Calculate(a,b)) { Win(); @@ -296,12 +304,14 @@ namespace ElementaryMathematics UpdateButtons(); rdoAddition.Select(); + lblNumCorrect.Text = "0"; + lblNumIncorrect.Text = "0"; } private void UpdateButtons() { - if (txtA.TextLength > 0 && txtB.TextLength > 0 && txtC.TextLength > 0) + if (txtA.TextLength > 0 && txtB.TextLength > 0 && txtC.TextLength > 0 && !hasTried) { btnChkAnswer.Enabled = true; @@ -344,5 +354,58 @@ namespace ElementaryMathematics } } + + private void btnShowCorrectAnswer_Click(object sender, EventArgs e) + { + + + ep1.Clear(); + + decimal a = 0; + decimal b = 0; + decimal c = 0; + + try + { + + a = decimal.Parse(txtA.Text); + + try + { + + b = decimal.Parse(txtB.Text); + + + try + { + + lblMessage.Text = Calculate(a, b).ToString(); + + } + catch (Exception exception) + { + ep1.SetError(btnChkAnswer, "Calculation Error"); + } + + + } + catch (Exception exception) + { + + ep1.SetError(txtB, "Invalid Value"); + + } + + } + catch (Exception exception) + { + + ep1.SetError(txtA, "Invalid Value"); + + } + + UpdateButtons(); + + } } } \ No newline at end of file