为什么我得到一个出现FormatException是未处理的错误?(Why am I getting

2019-07-03 17:15发布

我创建了一个程序,它的广泛的测试,我收到写着“出现FormatException是未处理的,输入字符串的不正确的格式”一个错误。 当我离开任何的文本框为空,然后按“完成”按钮,会出现问题,但如果我输入低于0或高于59的任何正常工作 - 这是我想允许的数量范围。 我能做什么,所以当盒子是空的,我不收到此错误? 这是我的背后代码“btnFinished”:

   private void btnFinished_Click(object sender, EventArgs e)
    {
        if (lstCyclists.SelectedIndex >= 0)
        {
            Cyclists currentCyc = (Cyclists)lstCyclists.SelectedItem;
            //Decalre the minsEntered and secsEntered variables for txtMins and textSecs
            int minsEntered = int.Parse(txtMins.Text);
            int secsEntered = int.Parse(txtSecs.Text);

            try
            {
                //If the status of a cyclist is already set to Finished, show an error
                if (currentCyc.Finished.ToString() == "Finished")
                {
                    MessageBox.Show("A time has already been entered for this cyclist");
                }
                else
                {
                    //if a minute lower than 0 or greater than 59 has been entered, show an error
                    if (minsEntered < 0 || minsEntered > 59)
                    {
                        MessageBox.Show("You can only enter a minute up to 59");
                    }
                    //if a second lower than 0 or greater than 59 has been entered, show an error
                    else if (secsEntered < 0 || secsEntered > 59)
                    {
                        MessageBox.Show("You can only enter a second up to 59");
                    }
                    else
                    {
                        //otherwise, set the status to finished and update the time
                        currentCyc.Finished = "Finished";
                        currentCyc.FinishedHours(Convert.ToInt32(txtHours.Text));
                        currentCyc.FinishedMins(Convert.ToInt32(txtMins.Text));
                        currentCyc.FinishedSecs(Convert.ToInt32(txtSecs.Text));
                        //pass the parameter to the scoreboard class to display it in lblCyclistsFinished
                        lblCyclistsFinished.Text += "\n" + finishLine.Scoreboard(currentCyc);
                        //add to the number of cyclists finished
                        Cyclists.NumFinished++;
                        lblnumFinished.Text = Cyclists.NumFinished.ToString();
                        //update the details box
                        DisplayDetails(currentCyc);
                        txtHours.Clear();
                    }
                }
            }
            catch
            //make sure all the time fields have been entered, otherwise show an error message
            {
                MessageBox.Show("Please ensure all time fields have been entered");
            }
        }
        else
            //make sure a cyclist has been selected when pressing "Finished", otherwise show an error message
        {
            MessageBox.Show("You must select a cyclist");
        }
    }

Answer 1:

好吧,看看这几行:

int minsEntered = int.Parse(txtMins.Text);
int secsEntered = int.Parse(txtSecs.Text);

你还指望那些返回时,文本框是空白的?

根本不叫int.Parse空文本框。 例如:

int minsEntered = txtMins.Text == "" ? 0 : int.Parse(txtMins.Text);
// Ditto for seconds

当然,这仍然会爆炸,如果你输入一些非数字。 你或许应该使用int.TryParse代替:

int minsEntered;
int.TryParse(txtMins.Text, out minsEntered);

在这里,我忽略了结果TryParse ,它会留下minsEntered为0反正-但是如果你想要一个不同的默认情况下,你会使用这样的:

int minsEntered;
if (!int.TryParse(txtMins.Text, out minsEntered))
{
    minsEntered = 5; // Default on parsing failure
}

(或者你也可以显示在这种情况下的错误信息......)



Answer 2:

当我离开的文本框为空的或者发生问题

这是个问题。 您正在使用int.Parse上一个空字符串。 空字符串不是有效的整数表示和解析失败。

您可以测试在文本框中的值和默认值,而不是合理的东西:

int minsEntered = 0;
int secsEntered = 0;

if(!string.IsNullOrWhitespace(textMins.Text))
{
  minsEntered = int.Parse(txtMins.Text);
}

if(!string.IsNullOrWhitespace(txtSecs.Text))
{
  secsEntered = int.Parse(txtSecs.Text);
}


Answer 3:

你可以使用int.TryParse,它返回一个布尔值,指示文本是否被成功解析:

int minsEntered = 0;
if (int.TryParse(txtMins.Text, out minsEntered))
{
    // txtMins.Text is a valid integer.
}


文章来源: Why am I getting a FormatException was unhandled error?