Vb.net 如何在vb网络中将标签和文本框相乘,然后在另一个标签中显示结果?
这就是我现在所拥有的,它触发了我一个错误“输入字符串格式不正确”。我是vb网络新手Vb.net 如何在vb网络中将标签和文本框相乘,然后在另一个标签中显示结果?,vb.net,Vb.net,这就是我现在所拥有的,它触发了我一个错误“输入字符串格式不正确”。我是vb网络新手 Dim total1a = Integer.Parse(lblPrice1a.Text) * Integer.Parse(txtQuantity1a.Text) Dim value As String = Convert.ToString(total1a) lblTotal1a.Text = value 你得到的错误 输入字符串格式不正确 表示您正在解析的两个字符串之一无法解析 Dim
Dim total1a = Integer.Parse(lblPrice1a.Text) * Integer.Parse(txtQuantity1a.Text)
Dim value As String = Convert.ToString(total1a)
lblTotal1a.Text = value
你得到的错误 输入字符串格式不正确 表示您正在解析的两个字符串之一无法解析
Dim total1a As Integer
Dim price As Integer
Dim quantity As Integer
Try
price = Cint(lblPrice1a)
Catch ex As Exception
'Code for whatever happens if it goes wrong
End Try
Try
quantity = Cint(lblQuantity1a)
Catch ex As Exception
'Code for whatever happens if it goes wrong
End Try
total1a = price*quantity
lblOutput.Text = Cstr(total1a)
这些try-catch语句可以防止在用户输入“popcorn”作为数量时抛出异常<根据我的经验,code>CInt与Integer.Parse()同样有效 您遇到的错误 输入字符串格式不正确 表示您正在解析的两个字符串之一无法解析
Dim total1a As Integer
Dim price As Integer
Dim quantity As Integer
Try
price = Cint(lblPrice1a)
Catch ex As Exception
'Code for whatever happens if it goes wrong
End Try
Try
quantity = Cint(lblQuantity1a)
Catch ex As Exception
'Code for whatever happens if it goes wrong
End Try
total1a = price*quantity
lblOutput.Text = Cstr(total1a)
这些try-catch语句可以防止在用户输入“popcorn”作为数量时抛出异常<根据我的经验,code>CInt与Integer.Parse()同样有效 试试下面的代码。最好使用
TryParse
方法进行数据类型转换。因为标签
是不可编辑的,所以明智的做法是else
code抛出异常
Dim price As Integer
Dim quantity As Integer
If Integer.TryParse(lblPrice1a.Text, price) Then
If Integer.TryParse(txtQuantity1a.Text, quantity) Then
lblTotal1a.Text = (price * quantity).ToString
Else
MessageBox.Show("Please enter valid quanity.")
End If
Else
Throw New Exception("lblPrice1a price is not an integer.")
End If
试试下面的代码。最好使用
TryParse
方法进行数据类型转换。因为标签
是不可编辑的,所以明智的做法是else
code抛出异常
Dim price As Integer
Dim quantity As Integer
If Integer.TryParse(lblPrice1a.Text, price) Then
If Integer.TryParse(txtQuantity1a.Text, quantity) Then
lblTotal1a.Text = (price * quantity).ToString
Else
MessageBox.Show("Please enter valid quanity.")
End If
Else
Throw New Exception("lblPrice1a price is not an integer.")
End If
您可以尝试
Val
而不是Integer.Parse。字符串是什么?如果您处理的是十进制数,则应使用Double.Parse()
或decimal.Parse()
。您可以尝试使用Val
而不是Integer.Parse。字符串是什么?如果您处理的是十进制数,则应使用Double.Parse()
或decimal.Parse()
。如果对区域设置有疑问,解析器会更好。虽然这在读取文本框值时可能并不重要,但在读取数字格式可能与当前用户设置不同的文件或其他源时更为重要。这是事实,但可能不会造成太大的差异,对吗?你是对的。。如果用户为计算机设置输入了错误的千或抽取字符。。。这只是一个你无能为力的PEBKAC错误。然而,我也强烈建议在文本框上使用键过滤器来限制用户的错误程度。。。LOL@Trevor谢谢,这确实教会了我一些东西。如果有关于区域设置的问题,解析器会更好。虽然这在读取文本框值时可能并不重要,但在读取数字格式可能与当前用户设置不同的文件或其他源时更为重要。这是事实,但可能不会造成太大的差异,对吗?你是对的。。如果用户为计算机设置输入了错误的千或抽取字符。。。这只是一个你无能为力的PEBKAC错误。然而,我也强烈建议在文本框上使用键过滤器来限制用户的错误程度。。。LOL@Trevor谢谢,这确实教会了我一些东西。