Dim labor, material, partstotal, labortotal, subtotal, tax, total As Decimal
material = Decimal.Parse(AmountTextBox.Text)
labor = Decimal.Parse(LaborTextBox.Text)
partstotal = material
labortotal = labor * 50
subtotal = labortotal + partstotal
tax = subtotal * 0.08
total = subtotal + tax
partstotal = Decimal.Parse(PartsTextBox.Text)
labortotal = Decimal.Parse(LaborTextBox.Text)
subtotal = Decimal.Parse(SubTotalTextBox.Text)
tax = Decimal.Parse(SalesTaxTextBox.Text)
total = Decimal.Parse(TotalTextBox.Text)
material = Decimal.Parse(AmountTextBox.Text)将无法运行。 为什么呢?
答案 0 :(得分:1)
文本框内的字符串值是什么?
TryParse()将返回错误而不是投掷。试试吧。
答案 1 :(得分:1)
我认为GregC所说的是你需要使用这样的代码:
Dim material As Decimal
If Not Decimal.TryParse(AmountTextBox.Text, material) Then
' the text in AmountTextBox could not be parsed as
' a Decimal.
'TODO: do something about it.
End If
虽然要验证很多项目,但您可以使用ErrorProvider Class提供更好的用户体验。