VB.Net 当我清除 PriceTxt 时有一个未处理的异常,只是因为 DiscountTxt 没有得到值



我的问题是,每当我在PriceTxt上输入价值价格时,DiscountTxt都会计算其折扣,但是当我清除PriceTxt时,它会收到一个异常,指出"从字符串"转换为类型'双精度'无效"。我尝试获取逻辑 if 语句,如果 PriceTxt.Text = " 折扣 Txt 将设置回 0,这意味着没有要计算的值。

Dim percentage As Decimal = 0.1
If PriceTxt.MaxLength > 11 Then
DiscountTxt.Text = (PriceTxt.Text * percentage)
DiscountTxt.Text = PriceTxt.Text - DiscountTxt.Text
End If
If PriceTxt.Text = "" Then
DiscountTxt.ResetText()
End If

没有想不出 Else if 和 Else 在上面,因为它也不起作用。

我相信你在这里看的是MaxLength而不是Length

这是另一种方法,使用TryParse. 它返回 IFTrue如果它能够将值压缩到目标中。 请参阅prc作为第一个。

Dim percentage As Decimal = 0.1
Dim prc as Decimal
If decimal.TryParse(PriceTxt.Text, prc) then 'puts it in a double, if it is.
DiscountTxt.Text = (prc * percentage)
Dim discount as Decimal
If decimal.TryParse(DiscountTxt.Text, discount) Then
DiscountTxt.Text = prc - discount 'Or is this a total box?
End If  
End If
If PriceTxt.Text = "" Then
DiscountTxt.ResetText()
End If

相关内容

最新更新