Visual Basic空文本框引发异常

时间:2016-10-19 03:52:27

标签: vb.net

以下代码是使用文本框计算BMI的程序。我遇到了一个问题,当我清除其中一个文本框时,它会抛出异常并冻结程序。我想知道是否有人对如何防止这种情况有答案。我已经尝试将我的变量设置为0和1以查看是否存在问题,但它似乎不是。

Private Sub tboxWeight_TextChanged(sender As Object, e As EventArgs) Handles tboxWeight.TextChanged
    Weight = 0
    Weight = Convert.ToInt64(tboxWeight.Text)
End Sub

Private Sub tboxHFeet_TextChanged(sender As Object, e As EventArgs) Handles tboxHFeet.TextChanged
    Height_feet = 0
    Height_feet = Convert.ToInt64(tboxHFeet.Text)
    Get_BMI(1)
End Sub

Private Sub tboxHInch_TextChanged(sender As Object, e As EventArgs) Handles tboxHInch.TextChanged
    Height_Inches = 0
    Height_Inches = Convert.ToInt64(tboxHInch.Text)
    Get_BMI(1)
End Sub



Private Sub tboxAge_TextChanged(sender As Object, e As EventArgs) Handles tboxAge.TextChanged
    Age = Convert.ToDouble(tboxAge.Text)

End Sub

Function Get_BMI(ByVal j As Integer) As Double
    BMI = (Weight / (Height_Inches + (Height_feet * 12) ^ 2) * 703)
    tboxBMI.Text = Convert.ToString(BMI)
    Exit Function
End function

2 个答案:

答案 0 :(得分:0)

这是因为您将文本框设置为整数字段,因此当文本框为空时,它将抛出异常,因为文本框不包含数字。 尝试为每个文本框使用If else语句。 pd.concat([s1, s2], axis=1).apply(lambda x: ordered_subset(*x), axis=1) 0 True 1 False dtype: bool 功能就足够了。

答案 1 :(得分:0)

好/最佳实践说,您需要在执行计算之前验证数据,即Get_BMI()。下面的代码片段可以帮助您。

Dim textBoxValue As String
If Not String.IsNullOrEmpty(textBoxValue) Then
    If IsNumeric(textBoxValue) Then

    End If
End If