Verify Answer button funtionality and keeping track of number correct and incorrect

This commit is contained in:
EggMan20339 2024-02-01 10:28:46 -05:00
parent 4b6397df50
commit edf110e774
2 changed files with 151 additions and 0 deletions

View File

@ -218,6 +218,7 @@
this.btnChkAnswer.TabIndex = 6; this.btnChkAnswer.TabIndex = 6;
this.btnChkAnswer.Text = "Check Answer"; this.btnChkAnswer.Text = "Check Answer";
this.btnChkAnswer.UseVisualStyleBackColor = true; this.btnChkAnswer.UseVisualStyleBackColor = true;
this.btnChkAnswer.Click += new System.EventHandler(this.btnChkAnswer_Click);
// //
// lblMessage // lblMessage
// //
@ -251,6 +252,7 @@
this.txtC.Name = "txtC"; this.txtC.Name = "txtC";
this.txtC.Size = new System.Drawing.Size(79, 20); this.txtC.Size = new System.Drawing.Size(79, 20);
this.txtC.TabIndex = 2; this.txtC.TabIndex = 2;
this.txtC.TextChanged += new System.EventHandler(this.txtC_TextChanged);
// //
// txtB // txtB
// //
@ -258,6 +260,7 @@
this.txtB.Name = "txtB"; this.txtB.Name = "txtB";
this.txtB.Size = new System.Drawing.Size(79, 20); this.txtB.Size = new System.Drawing.Size(79, 20);
this.txtB.TabIndex = 1; this.txtB.TabIndex = 1;
this.txtB.TextChanged += new System.EventHandler(this.txtB_TextChanged);
// //
// txtA // txtA
// //
@ -265,6 +268,7 @@
this.txtA.Name = "txtA"; this.txtA.Name = "txtA";
this.txtA.Size = new System.Drawing.Size(79, 20); this.txtA.Size = new System.Drawing.Size(79, 20);
this.txtA.TabIndex = 0; this.txtA.TabIndex = 0;
this.txtA.TextChanged += new System.EventHandler(this.txtA_TextChanged);
// //
// chkShowSummary // chkShowSummary
// //

View File

@ -19,6 +19,11 @@ namespace ElementaryMathematics
private bool isMultiply = false; private bool isMultiply = false;
private bool isDivide = false; private bool isDivide = false;
private Int32 numRunningCorrect = 0;
private Int32 numRunningIncorrect = 0;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
@ -102,5 +107,147 @@ namespace ElementaryMathematics
{ {
Close(); 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();
}
} }
} }