【发布时间】:2014-12-07 03:58:31
【问题描述】:
我正在模拟自动售货机,并希望将产品数量文本框设置为仅接受大于 0 的值。当我输入 -1 时,我的程序接受该值并显示它我不想要。有人可以帮忙吗
代码是:
//create a new Employee object
try // Exception handling to ensure that incorrect data type cannot be entered into text box creating a new product
{
Products newProd = new Products(this.textProdID.Text);
newProd.ProductName= this.textProdName.Text;
newProd.ProductQuantity= Convert.ToInt32(this.textProdQuantity.Text);
newProd.ProductPrice= Convert.ToDouble(this.textProdPrice.Text);
ProductList.Add(newProd);
MessageBox.Show(newProd.ProdName + " has been added to the product list");
}
catch
{
MessageBox.Show("Format entered into text box Is incorrect please check and try again");
}
【问题讨论】:
-
你的票数可能会下降,因为你甚至没有明确地提出问题
-
您的代码不包含指定的数据范围验证(>0);如果验证失败,您必须添加该行并抛出 ArgumentException。最好的问候,
-
如何检查输入的值是否 > 0?
标签: c# winforms validation try-catch tryparse