Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在循环中检查文本框中的有效输入或尝试catch c#_C#_Forms_Exception Handling - Fatal编程技术网

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

在循环中检查文本框中的有效输入或尝试catch c#,c#,forms,exception-handling,C#,Forms,Exception Handling,我在验证用户输入时遇到问题,下面的循环肯定会捕获它,但是当循环再次启动时,用户没有机会输入不同的值,因此值是相同的,只是创建了一个无休止的循环 private void guess_Click(object sender, EventArgs e) { int guessInt = 0; bool pass = false; int number; while (pass == false)

我在验证用户输入时遇到问题,下面的循环肯定会捕获它,但是当循环再次启动时,用户没有机会输入不同的值,因此值是相同的,只是创建了一个无休止的循环

              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尝试?

似乎不需要一段时间:

          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 = "";
              }
           }
如果还想验证空值,只需在以下情况下删除第一个:

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

一旦用户单击messagebox上的OK,循环将再次运行,而不会给用户更改值的机会

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

下面是一篇关于使用验证回调的示例和短文:

在该示例中,请参见:

  • private void button保存\u单击
    -这是您放置消息框的位置,并且

  • private void textBoxNumber\u验证
    -在这里,您可以放置您的
    pass=Int32.TryParse(猜输入.Text,输出编号).
    code


您是否尝试过使用
制动来制动环路
然后在用户输入其他值时再次启动循环。@I.am.WritZ,如果我中断循环,特别是在一次失败的尝试或else部分中,我不想在循环后继续使用无效的条目。因此
中断
循环并
返回
无效
,或者您可以维护一个
bool
,它将保存循环的成功/失败。@programmerNOOB您能显示完整的代码吗?我添加了整个代码方法/事件我可以使用它,但问题是,如果输入不是空的,它会跳过所有输入,并且输入永远不会分配给我需要的值。这是按钮单击方法中的一部分,用户在文本框中输入一个数字,然后按下此按钮,我正在尝试验证它。很好。那么,我提供的代码有什么问题?如果文本是某物,它将解析它并检查它是否为有效数字。如果没有,则显示文本并在文本框中留空。看起来还可以。那我怎么解释一个空白条目呢?你想验证它吗?只要去掉第一个。我会更新代码的,我只是需要一个嵌套的if来覆盖我所有的场地,谢谢!
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++;