使用Try / Parse验证用户输入?

时间:2015-02-06 14:52:31

标签: c# tryparse

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;
}

当我运行程序时,它告诉我必须输入客户名称,即使我输入了它。我该如何改变?

1 个答案:

答案 0 :(得分:2)

替换

if (!char.TryParse(txtCustomerName.Text, out charCustomerName)) 

if (String.IsNullOrEmpty(txtCustomerName.Text)) 

图表仅代表一个字符