为什么我会收到出现FormatException是未处理的错误? [英] Why am I getting a FormatException was unhandled error?
问题描述
我创建了一个程序,它的广泛的测试,我收到写着FormatException是未处理的,输入字符串的不正确的格式一个错误。当我离开任何的文本框为空,然后按完成按钮,会出现问题,但如果我输入低于0或高于59的任何正常工作 - 这是我想允许的数字范围。我能做什么,所以当盒子是空的我没有收到这个错误?这背后是btnFinished我的代码:
私人无效btnFinished_Click(对象发件人,EventArgs五)
{
如果(lstCyclists.SelectedIndex> = 0)
{
骑自行车currentCyc =(骑自行车)lstCyclists.SelectedItem;
// Decalre的minsEntered和txtMins和textSecs
INT minsEntered = int.Parse(txtMins.Text)secsEntered变量;
INT secsEntered = int.Parse(txtSecs.Text);
试
{
//如果一个骑自行车的状态已被设置为完成,显示错误
如果(currentCyc.Finished.ToString()== 已完成)
{
MessageBox.Show(A时间已经进入了这个骑自行车的人);
}
,否则
{
//如果低于0或大于59一分钟已输入,显示一个错误
如果(minsEntered℃,|| minsEntered> 59)
{
MessageBox.Show(你只能输入分钟高达59);
}
//如果一第二低于0或大于59已输入,显示一个错误
,否则如果(secsEntered℃,|| secsEntered> 59)
{
MessageBox.Show(你只能输入第二个最多59);
}
,否则
{
//否则,将状态设置为完成和更新时间
currentCyc.Finished =完成;
currentCyc.FinishedHours(Convert.ToInt32(txtHours.Text));
currentCyc.FinishedMins(Convert.ToInt32(txtMins.Text));
currentCyc.FinishedSecs(Convert.ToInt32(txtSecs.Text));
//传递参数给记分牌类lblCyclistsFinished
lblCyclistsFinished.Text显示它+ =\\\
+ finishLine.Scoreboard(currentCyc);
//添加到骑自行车的人的数量完成
Cyclists.NumFinished ++;
lblnumFinished.Text = Cyclists.NumFinished.ToString();
//更新的详细信息框
DisplayDetails(currentCyc);
txtHours.Clear();
}
}
}
抓
//确保所有的时间字段已输入,否则显示错误消息
{
的MessageBox.show(请确保所有的时间字段已经进入了);
}
}
,否则
//确保当按下完成一个骑自行车已被选中,否则显示错误信息
{
的MessageBox。显示(你必须选择一个骑自行车的人);
}
}
好,看看这些行:
INT minsEntered = int.Parse(txtMins.Text);
INT secsEntered = int.Parse(txtSecs.Text);
你是什么期望的那些返回时,文本框是空白的?
只要不叫 int.Parse
空文本框。例如:
INT minsEntered = txtMins.Text ==? 0:int.Parse(txtMins.Text);
//同上,用于秒
当然,这仍然会爆炸,如果你输入的东西非数字。你或许应该使用 int.TryParse
而不是:
INT minsEntered;
int.TryParse(txtMins.Text,出minsEntered);
在这里,我忽略了的TryParse
,它会留下 minsEntered
0作为反正 - 但是如果你想要一个不同的默认情况下,你会使用这样的:
INT minsEntered;
如果(int.TryParse(txtMins.Text,出minsEntered)!)
{
minsEntered = 5; //在解析失败
}
默认
(或者你也可以显示一个错误消息案例......)
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 += "\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");
}
}
Well, look at these lines:
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?
Simply don't call int.Parse
for empty textboxes. For example:
int minsEntered = txtMins.Text == "" ? 0 : int.Parse(txtMins.Text);
// Ditto for seconds
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);
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是未处理的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!