if (!char.TryParse(txtCustomerName.Text, out charCustomerName)) //try to read in Layers from TextBox
{
MessageBox.Show("Customer Name must be entered.", "Invalid Customer Name");
return;
}
if (!Decimal.TryParse(txtAmountOwed.Text, out decAmountOwed)) //try to read in Layers from TextBox
{
MessageBox.Show("Amount Owed must be entered without dollar signs or commas.", "Invalid Amount Owed");
return;
}
if (!int.TryParse(txtDaysOverdue.Text, out intDaysOverdue)) //try to read in Layers from TextBox
{
MessageBox.Show("Days Overdue must be entered as a whole number.", "Invalid Days Overdue");
return;
}
當我運行該程序時,它告訴我,即使我輸入了客戶名稱,也必須輸入客戶名稱。我該如何改變它?使用Try/Parse驗證用戶輸入?
只是好奇,爲什麼你想使用'char.TryParse'?這將試圖將字符串(文本)解析爲一個「char」類型,它是一個單獨的字符。 – ryanyuyu 2015-02-06 14:54:49
您正試圖從一串字符中解析單個字符。難道你不需要'!string.IsNullOrEmpty(txtCustomerName.Text)'? – Andrei 2015-02-06 14:55:19
Andrei,IsNullOrWhiteSpace更好 – Bauss 2015-02-06 14:58:30