模擬自動售貨機,並希望將產品數量文本框設置爲只接受大於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