【发布时间】:2018-04-24 14:25:03
【问题描述】:
表单有2个列表框,一个用于输入衬衫尺寸和价格,另一个用于显示原价,点击一个按钮后,第二个列表框将原价转换为折扣价(仅用于价格大于 100) 可享受 10% 的折扣。这部分代码给了我一个错误“无法从字符串转换为十进制” For i As Integer = 0 To ListBox2.Items.Count - 1 getdiscountedprice(decprice) Decimal.TryParse(ListBox2.Items(i), decprice)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strShirt As String
Dim strprice As String
Dim blnmore As Boolean = True
Do While blnmore
strShirt = InputBox("Enter shirt: ")
strprice = InputBox(" Enter shirt price: ")
If strShirt = String.Empty Or strprice = String.Empty Then
blnmore = False
End If
ListBox1.Items.Add(strShirt & " " & strprice)
ListBox2.Items.Add(strprice)
Loop
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim decprice As Decimal
For i As Integer = 0 To ListBox2.Items.Count - 1
getdiscountedprice(decprice)
Decimal.TryParse(ListBox2.Items(i), decprice)
Next
End Sub
Private Function getdiscountedprice(ByVal Fstrprice As Integer) As Decimal 'cause decimal will be returned
Dim lastprice As Decimal
If Fstrprice > 100 Then
lastprice = Fstrprice * 0.9
End If
Return (lastprice)
End Function
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Close()
End Sub
【问题讨论】:
-
Decimal.TryParse您的标题与您的变量或您的错误不匹配。 -
@LarsTech 你是什么意思?
-
字符串到整数?您的图像显示小数,而不是整数。不要发布代码或异常的图像,我们希望在问题中发布文本。
-
@LarsTech 问题已添加,请查看