0

I am having trouble validating user input, I have the following loop which definitely catches it but when the loop starts again the user doesn't have a chance to enter a different value so the value is the same and just creates an endless loop.

              private void guess_Click(object sender, EventArgs e)
    {


        int guessInt = 0;

        bool pass = false;
        int number;
        while (pass == false)
        {
            if (guessInput.Text != "")
            {
                pass = Int32.TryParse(guessInput.Text, out number);
                if (pass)
                {
                    guessInt = number;
                }
                else
                {
                    MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    guessInput.Text = "";
                }
            }
            else MessageBox.Show("You did not enter anything, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        guess.Enabled = false;
        next_Guess.Enabled = true;

        if (guessInt == randomArray[x])
        {
            result.Text = ("You Win! The correct number was " + randomArray[x]);
            right += 1;
            correctAnswers.Text = right.ToString();

        }
        else
        {
            result.Text = ("Sorry you lose, the number is " + randomArray[x]);
            wrong += 1;
            incorrectAnswers.Text = wrong.ToString();

        }

        hintLabel.Enabled = false;
        x++;
    }

So how can the user have a chance to reenter a value and the loop start again or should I be using a try/catch attempt here?

5
  • have you tried braking the loop with break; and then start the loop again when user enters a different value. Commented May 16, 2012 at 3:29
  • @I.am.WritZ, if I break the loop, especially during a failed attempt or the else portion I don't want to continue after the loop with a invalid entry. Commented May 16, 2012 at 3:33
  • So break the loop and return the void or you can maintain a bool which will hold the success/fail of the loop. Commented May 16, 2012 at 3:37
  • @programmerNOOB can you show your full code? Commented May 16, 2012 at 3:52
  • I added the whole method/event Commented May 16, 2012 at 4:12

3 Answers 3

1
int number;
if(string.IsNullOrEmpty(guessInput.Text))
{
  MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
  return;
}
if(Int32.TryParse(guessInput.Text, out number))
{
  guessInt = number; 
}else
{
  MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values",      MessageBoxButtons.OK, MessageBoxIcon.Error);
   guessInput.Text = "";
   return;
}


// when come to here you have guessInt, process it 

   guess.Enabled = false;
    next_Guess.Enabled = true;

    if (guessInt == randomArray[x])
    {
        result.Text = ("You Win! The correct number was " + randomArray[x]);
        right += 1;
        correctAnswers.Text = right.ToString();

    }
    else
    {
        result.Text = ("Sorry you lose, the number is " + randomArray[x]);
        wrong += 1;
        incorrectAnswers.Text = wrong.ToString();

    }

    hintLabel.Enabled = false;
    x++;
Sign up to request clarification or add additional context in comments.

1 Comment

That did it, I just needed a nested if to cover all my grounds, thank you!
1

It seems you don't need a while there:

          int number;

          if(guessInput.Text != "")
          {
              var pass = Int32.TryParse(guessInput.Text, out number);
              if (pass)
              {
                 guessInt = number;        
              }
              else
              {
                 MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 guessInput.Text = "";
              }
           }

if you want to validate also for empty values, just remove the first if:

          int number;

          var pass = Int32.TryParse(guessInput.Text, out number);
          if (pass)
          {
             guessInt = number;        
          }
          else               {
             MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
             guessInput.Text = "";
          }

7 Comments

I could play with this but the problem is that if the input is not blank it just skips it all and the input is never assigned to the value I need.
this is a portion within a button click method where the user enters a number in a textbox and presses this button and I am trying to validate it.
Great. So, what is wrong with the code I've provided? If the text is something, it parses it and checks if it's a valid number. if not, it shows the text and blanks the textbox. It seems ok.
how do I account for a blank entry then?
You want to validate it? just remove the first if. I'll update the code for that
|
0

As soon as the user clicks OK on the messagebox, the loop is going to run again without giving the user a chance to change the value.

What you need to do is run validation only when they enter a guess. That is, rather than have a loop, have some code that is triggered by an event (such as clicking a button) or by the validation callback provided by winforms.

Here's an example and short article I found on using the validation callback: http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx

In that example, see:

  • private void buttonSave_Click - this is where you would put your messagebox, and

  • private void textBoxNumber_Validating - this is where you would put your pass = Int32.TryParse(guessInput.Text, out number)... code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.