[英]Validate user input with multiple criteria c#
我需要根據是否為數字以及是否超出庫存水平來驗證輸入。
到目前為止,我可以檢查庫存水平,但是我只知道如何使用tryparse來檢查其是否為數字,並且我不想輸出任何內容。 到目前為止,這就是我所擁有的。 因為我沒有給它一個變量,所以它給出了一個錯誤。
if (ckbSingle.IsChecked.Value)
{
if (int.TryParse(txtSingQuan.Text, out Convert.ToInt32(txtSingQuan.Text)))
if ((singleespresso.DunkinInventory - Convert.ToInt32(txtSingQuan.Text)) <= 0)
{
MessageBox.Show("Espresso low in stock.");
}
else
{
ProductList.Add("Single Espresso");
}
}
我希望它允許我在輸入正確的情況下繼續執行代碼,在不合適的情況下顯示一個消息框。
它給出了一個錯誤,因為我沒有給它一個變量
然后給它一個:
if (ckbSingle.IsChecked.Value)
{
if (int.TryParse(txtSingQuan.Text, out int qty) && qty >= singleespresso.DunkinInventory)
{
// Input is a valid number and is greater than or equals to stock
ProductList.Add("Single Espresso");
// qty variable is accessible in that scope if need be
}
else
{
// Input is either not a number or lower than stock
MessageBox.Show("Espresso low in stock.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.