我有一个使用Visual Studio 2013用Visual Basic编写的单位转换器。程序运行正常,直到用户输入零(0)或以小数(。)开头。我收到此错误消息:从字符串“”到“Decimal”类型的转换无效。如何让程序接受零点和/或小数点作为用户的初始输入而不会导致程序崩溃?代码如下。
Private Function GetLength1(ByVal decLengthUnit1 As Decimal) As Decimal
Dim decResult1 As Decimal
If cboUnitType.SelectedItem = "Length" Then
' converts kilometer to...
If cbo1.SelectedItem = "Kilometer" Then
If cbo2.SelectedItem = "Kilometer" Then
decResult1 = txtUnit1.Text
ElseIf cbo2.SelectedItem = "Meter" Then
decResult1 = (decLengthUnit1 * 1000)
ElseIf cbo2.SelectedItem = "Centimeter" Then
decResult1 = (decLengthUnit1 * 100000)
ElseIf cbo2.SelectedItem = "Millimeter" Then
decResult1 = (decLengthUnit1 * 1000000)
ElseIf cbo2.SelectedItem = "Mile" Then
decResult1 = (decLengthUnit1 * 0.621371191)
ElseIf cbo2.SelectedItem = "Yard" Then
decResult1 = (decLengthUnit1 * 1093.613297)
ElseIf cbo2.SelectedItem = "Foot" Then
decResult1 = (decLengthUnit1 * 3280.83989)
ElseIf cbo2.SelectedItem = "Inch" Then
decResult1 = (decLengthUnit1 * 39370.07868)
End If
End If
End If
If decResult1 < 0 Then
txtUnit2.ForeColor = Color.Red
ElseIf decResult1 >= 0 Then
txtUnit2.ForeColor = Color.Green
End If
Return decResult1.ToString().Trim("0")
End Function
Private Sub txtUnit1_TextChanged(sender As Object, e As EventArgs) Handles txtUnit1.TextChanged
If suppressTextBox1TextChanged = False Then
' convert string to numeric data type
Decimal.TryParse(txtUnit1.Text, decUnit1)
' handle String.Empty, or negative sign
If txtUnit1.Text = "" OrElse txtUnit1.Text = "-" Then
txtUnit2.Text = ""
ElseIf cboUnitType.SelectedItem = "Length" Then
suppressTextBox2TextChanged = True
txtUnit2.Text = GetLength1(decUnit1)
suppressTextBox2TextChanged = False
ElseIf cboUnitType.SelectedItem = "Pressure" Then
ElseIf cboUnitType.SelectedItem = "Temperature" Then
suppressTextBox2TextChanged = True
txtUnit2.Text = GetTemp1(decUnit1)
suppressTextBox2TextChanged = False
End If
End If
End Sub
答案 0 :(得分:1)
使用System.Convert.ToDecimal()
而不是仅仅将文本框值分配给Decimal
变量,如下所示:
decResult1 = Convert.ToDecimal(txtUnit1.Text)
正如艾哈迈德在评论中所指出的那样,使用Decimal.TryParse()
将允许你避免在演员表失败时System.Convert.ToDecimal()
将抛出的异常,如下所示:
Dim number As Decimal
If Decimal.TryParse(txtUnit1.Text, number) Then
' Successfully cast text box text to a Decimal
Else
' Unable to cast text box text to a Decimal
End If
更新:
以下是您的代码编辑:
Private Function GetLength1(ByVal decLengthUnit1 As Decimal) As Decimal
Dim decResult1 As Decimal
If cboUnitType.SelectedItem = "Length" Then
' converts kilometer to...
If cbo1.SelectedItem = "Kilometer" Then
If cbo2.SelectedItem = "Kilometer" Then
Decimal.TryParse(txtUnit1.Text, decResult1)
ElseIf cbo2.SelectedItem = "Meter" Then
decResult1 = (decLengthUnit1 * 1000)
ElseIf cbo2.SelectedItem = "Centimeter" Then
decResult1 = (decLengthUnit1 * 100000)
ElseIf cbo2.SelectedItem = "Millimeter" Then
decResult1 = (decLengthUnit1 * 1000000)
ElseIf cbo2.SelectedItem = "Mile" Then
decResult1 = (decLengthUnit1 * 0.621371191)
ElseIf cbo2.SelectedItem = "Yard" Then
decResult1 = (decLengthUnit1 * 1093.613297)
ElseIf cbo2.SelectedItem = "Foot" Then
decResult1 = (decLengthUnit1 * 3280.83989)
ElseIf cbo2.SelectedItem = "Inch" Then
decResult1 = (decLengthUnit1 * 39370.07868)
End If
End If
End If
If decResult1 < 0 Then
txtUnit2.ForeColor = Color.Red
ElseIf decResult1 >= 0 Then
txtUnit2.ForeColor = Color.Green
End If
Return decResult1.ToString().Trim("0")
End Function