简体   繁体   English

在循环中检查文本框中的有效输入或尝试捕获c#

[英]Checking textbox for valid input in a loop or try catch c#

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? 那么用户如何有机会重新输入一个值并重新开始循环,或者我应该在这里使用try / catch尝试?

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: 如果你想验证空值,只需删除第一个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 = "";
          }
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++;

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. 也就是说,不是有一个循环,而是有一些由事件(例如单击按钮)或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 这是我在使用验证回调时发现的一个示例和简短文章: 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 buttonSave_Click - 这是你放置消息框的地方,和

  • private void textBoxNumber_Validating - this is where you would put your pass = Int32.TryParse(guessInput.Text, out number)... code. private void textBoxNumber_Validating - 这是你把pass = Int32.TryParse(guessInput.Text, out number)...代码放到的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM