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

时间:2012-05-16 03:26:49

标签: c# forms exception-handling

我无法验证用户输入,我有以下循环肯定会捕获它但是当循环再次启动时,用户没有机会输入不同的值,所以值是相同的,只是创造了一个无穷无尽的循环。

              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++;
    }

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

3 个答案:

答案 0 :(得分:1)

似乎你不需要一段时间:

          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:

          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 = "";
          }

答案 1 :(得分: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++;

答案 2 :(得分:0)

只要用户在消息框上单击“确定”,循环就会再次运行,而不会让用户有机会更改该值。

您需要做的是仅在输入猜测时运行验证。也就是说,不是有一个循环,而是有一些由事件触发的代码(例如单击按钮)或winforms提供的验证回调。

这是我在使用验证回调时发现的一个示例和简短文章: http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx

在该示例中,请参阅:

  • private void buttonSave_Click - 这是您放置消息框的地方,

  • private void textBoxNumber_Validating - 这是您放置pass = Int32.TryParse(guessInput.Text, out number)...代码的地方。