未设置值时,单击清除表单按钮不会导致异常,但在清除前插入数字值时会返回异常。其他带有字符串变量的文本框工作正常。清除已填充的数字文本框时输入异常
private void txtIngred4Sugar_TextChanged(object sender, EventArgs e)
{
float ing4S = (float)Convert.ToDouble(txtIngred4Sugar.Text);
}
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else
ClearAllText(c);
}
}
private void clearForm1_Click(object sender, EventArgs e)
{
ClearAllText(this);
}
例外: 类型“System.FormatException”的未处理的异常出现在mscorlib.dll
其他信息:输入字符串的不正确的格式。
什么是异常消息? – Partha
这是一个令人难以置信的特定错误消息。 'txtIngred4Sugar.Text'中的值不能转换为double。检查抛出异常时的值。 –
所以清除盒子使其为0.而0是不正确的输入我猜测。我最后试图能够接受像'20'这样的整数,并且能够将一些小数值(如20.5)用作良好的用户输入。 – DV8DUG