Cindy
Cindy

Reputation: 59

How to let the application check a users input?

I am making an Addition Tutoring sample, and I cannot finds ways to check for a user's input. I know there are ways where you could do a compare contrast on little things like, when a student's grade is 90-100, maybe you could apply a MessageBox.Show indicating that this student's grade is considered as an A in most cases. But, I cannot figure out which attempt to use when you are checking for an addition's sum. Like, the snippet below will generate new problems for a user to work on.

private void Newproblem_Click(object sender, EventArgs e)
{
  Random Numbers = new Random();
  int number1;
  int number2;
  int Sum;

  number1 = Numbers.Next(400) + 101;
  number2 = Numbers.Next(400) + 101;
  theproblemLabel.Text = number1 + "  +  " + number2.ToString();
}

But I want to be able to check a user's answers as well. Will someone provide me an approach on how to make that happen? I will greatly appreciate any hints anyone could give me.

Upvotes: 1

Views: 127

Answers (2)

Random IT Guy
Random IT Guy

Reputation: 623

A better way would be to use a textbox for the question and another textbox for the answer.

Btw if you have used the properties before, you could use the property: ReadOnly and set it to true, so the user cannot modify the problem.

Layout with different situations: Not a number Not correct Correct

Example:

//Declare variables so you can use them globally
int number1, number2, sum, userSolution;
Random numbers;

private void btnProblem_Click(object sender, EventArgs e)
{
  numbers = new Random();
  number1 = numbers.Next(400) + 101;
  number2 = numbers.Next(400) + 101;

  sum = number1 + number2;
  txtProblem.Text = number1 + "  +  " + number2;
}

private void btnSolution_Click(object sender, EventArgs e)
{
  // You try to parse the text to a integer,
  // if it works its stored in userSolution,
  // If it fails, it shows the messagebox
  if (!int.TryParse(txtSolution.Text, out userSolution))
  {
    MessageBox.Show("Input is not a valid number.");
  }
  else
  {
    // Check user solution and compare it to the sum
    if (userSolution == sum)
    {
      MessageBox.Show("Correct!", "Problem Solved!");
    }
    else
    {
      MessageBox.Show("Not Correct.", "Please try again.");
    }
  }
}

Upvotes: 3

Steve
Steve

Reputation: 216343

You could store the answer in the Tag property of the textbox

number1 = Numbers.Next(400) + 101;
number2 = Numbers.Next(400) + 101;
int answer = number1 + number2;

theproblemLabel.Text = string.Format("{0} + {1}", number1, number2);
theproblemLabel.Tag = answer;

then, when the user clicks a button to confirm its answer, you check against the stored Tag

private void Answer_Click(object sender, EventArgs e)
{
  int userAnswer;
  if(!Int32.TryParse(txtAnswer.Text, out userAnswer))
    MessageBox.Show("Please enter a number!");
  else
  {
    if(userAnswer == Convert.ToInt32(theproblemLabel.Tag))
      MessageBox.Show("Correct answer!");
    else
      MessageBox.Show("Wrong answer, try againg!");
  }
}

I am supposing you have a TextBox called txtAnswer where the user types its answer and a button called Answer clicked to confirm the answer

Upvotes: 1

Related Questions