模拟自动售货机,并希望将产品数量文本框设置为只接受大于0的值。当我输入-1时,我的程序接受此值并显示这其中我不want.can有人帮助,请将输入数据验证添加为只接受大于0的整数值
代码:
//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");
}
您可能因为没有明确提出问题而拒绝投票 – logixologist 2014-12-07 04:01:37
您的代码不包含指定的数据范围验证(> 0);您必须添加该行并在验证失败时抛出ArgumentException。最好的问候, – 2014-12-07 04:01:49
你是否检查输入的值是否大于0? – logixologist 2014-12-07 04:02:43