diff --git a/ElementaryMathematics/Form1.Designer.cs b/ElementaryMathematics/Form1.Designer.cs index 33f7b36..c91d664 100644 --- a/ElementaryMathematics/Form1.Designer.cs +++ b/ElementaryMathematics/Form1.Designer.cs @@ -218,6 +218,7 @@ this.btnChkAnswer.TabIndex = 6; this.btnChkAnswer.Text = "Check Answer"; this.btnChkAnswer.UseVisualStyleBackColor = true; + this.btnChkAnswer.Click += new System.EventHandler(this.btnChkAnswer_Click); // // lblMessage // @@ -251,6 +252,7 @@ this.txtC.Name = "txtC"; this.txtC.Size = new System.Drawing.Size(79, 20); this.txtC.TabIndex = 2; + this.txtC.TextChanged += new System.EventHandler(this.txtC_TextChanged); // // txtB // @@ -258,6 +260,7 @@ this.txtB.Name = "txtB"; this.txtB.Size = new System.Drawing.Size(79, 20); this.txtB.TabIndex = 1; + this.txtB.TextChanged += new System.EventHandler(this.txtB_TextChanged); // // txtA // @@ -265,6 +268,7 @@ this.txtA.Name = "txtA"; this.txtA.Size = new System.Drawing.Size(79, 20); this.txtA.TabIndex = 0; + this.txtA.TextChanged += new System.EventHandler(this.txtA_TextChanged); // // chkShowSummary // diff --git a/ElementaryMathematics/Form1.cs b/ElementaryMathematics/Form1.cs index 8e6b13b..11f183f 100644 --- a/ElementaryMathematics/Form1.cs +++ b/ElementaryMathematics/Form1.cs @@ -18,6 +18,11 @@ namespace ElementaryMathematics private bool isSubtract = false; private bool isMultiply = false; private bool isDivide = false; + + private Int32 numRunningCorrect = 0; + private Int32 numRunningIncorrect = 0; + + public Form1() { @@ -102,5 +107,147 @@ namespace ElementaryMathematics { Close(); } + + private ErrorProvider ep1 = new ErrorProvider(); + + private decimal Calculate(decimal a, decimal b, decimal c) + { + + decimal answer = 0; + + + try + { + + if (isAdd) + { + answer = a + b; + }else if (isSubtract) + { + answer = a - b; + }else if (isMultiply) + { + answer = a * b; + }else if (isDivide) + { + answer = a / b; + } + else + { + throw new Exception(); + } + + } + catch (Exception e) + { + MessageBox.Show("Whoops!"); + } + + + return answer; + } + + private void Win() + { + + lblMessage.Text = "Good Job!"; + numRunningCorrect++; + + } + + private void Fail() + { + + lblMessage.Text = "Try Again"; + numRunningIncorrect++; + + } + + + private void btnChkAnswer_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 + { + + c = decimal.Parse(txtC.Text); + + try + { + + if (c == Calculate(a,b,c)) + { + + Win(); + + } + else + { + Fail(); + } + + } + catch (Exception exception) + { + ep1.SetError(btnChkAnswer, "Calculation Error"); + } + + + } + catch (Exception exception) + { + + ep1.SetError(txtC, "Invalid Value"); + + } + + } + catch (Exception exception) + { + + ep1.SetError(txtB, "Invalid Value"); + + } + + } + catch (Exception exception) + { + + ep1.SetError(txtA, "Invalid Value"); + + } + + } + + private void txtA_TextChanged(object sender, EventArgs e) + { + ep1.Clear(); + } + + private void txtB_TextChanged(object sender, EventArgs e) + { + ep1.Clear(); + } + + private void txtC_TextChanged(object sender, EventArgs e) + { + ep1.Clear(); + } } } \ No newline at end of file