为什么我收到 FormatException was unhandled 错误? [英] Why am I getting a FormatException was unhandled error?

查看:25
本文介绍了为什么我收到 FormatException was unhandled 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,并对其进行了广泛的测试,我收到一条错误消息,提示FormatException 未处理,输入字符串格式不正确".当我将任何一个文本框留空并按下完成"按钮时,就会出现问题,但如果我输入低于 0 或高于 59 的任何值(这是我想要允许的数字范围),它就可以正常工作.我该怎么做才能在框为空白时不会收到此错误?这是我在 'btnFinished' 后面的代码:

I have created a program, and a extensive test of it, I'm getting a error that says "FormatException was unhandled, Input string was not in a correct format". The problem occurs when I leave either of the text boxes blank and press the 'Finished' button but it works fine if I enter anything below 0 or above 59 - which is the number range I want to allow. What could I do so I don't receive this error when the boxes are blank? This is my code behind '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 += "
" + 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");
        }
    }

推荐答案

好吧,看看这些行:

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

当文本框为空白时,您期望返回什么?

What do you expect those to return when the text boxes are blank?

简单地不要为空文本框调用 int.Parse.例如:

Simply don't call int.Parse for empty textboxes. For example:

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

当然,如果您输入非数字内容,这仍然会出错.您可能应该使用 int.TryParse 代替:

Of course, this will still go bang if you enter something non-numeric. You should probably be using int.TryParse instead:

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

这里我忽略了 TryParse 的结果,无论如何它都会将 minsEntered 保留为 0 - 但如果你想要一个不同的默认值,你会使用类似的东西:

Here I'm ignoring the result of TryParse, and it will leave minsEntered as 0 anyway - but if you wanted a different default, you'd use something like:

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

(或者您可以在这种情况下显示错误消息...)

(Or you can show an error message in that case...)

这篇关于为什么我收到 FormatException was unhandled 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆