解析文本框时获取无效的强制转换异常

时间:2012-08-11 14:51:23

标签: vb.net

Error Image

嗯,上面的链接会告诉你,但我有一些文本框,当输入数字时,它们都会自动汇总并在一个单独的框中显示总和。以下是我在以下文本框中显示总和的代码:

Try
        Dim One As Integer
        Dim two As Integer
        Dim three As Integer
        Dim four As Integer
        Dim five As Integer
        Dim six As Integer
        Dim seven As Integer
        If CDbl(txtMon1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtTues1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtWed1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtThurs1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtFri1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtSat1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtSun1.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf String.IsNullOrEmpty(txtMon1.Text) Then
            One = CInt(0)
            two = CInt(0)
            three = CInt(0)
            four = CInt(0)
            five = CInt(0)
            six = CInt(0)
            seven = CInt(0)
        ElseIf Not IsNumeric(txtMon1.Text) Then
            One = CInt(0)
            two = CInt(0)
            three = CInt(0)
            four = CInt(0)
            five = CInt(0)
            six = CInt(0)
            seven = CInt(0)
        Else
            One = CInt(Convert.ToInt64(txtMon1.Text))
            two = CInt(Convert.ToInt64(txtTues1.Text))
            three = CInt(Convert.ToInt64(txtWed1.Text))
            four = CInt(Convert.ToInt64(txtThurs1.Text))
            five = CInt(Convert.ToInt64(txtFri1.Text))
            six = CInt(Convert.ToInt64(txtSat1.Text))
            seven = CInt(Convert.ToInt64(txtSun1.Text))
            txtTot1.Text = CStr(Math.Round(One + two + three + four + five + six + seven))
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub txtTot2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTot2.TextChanged, txtMon2.TextChanged, txtTues2.TextChanged, txtWed2.TextChanged, txtThurs2.TextChanged, txtFri2.TextChanged, txtSat2.TextChanged, txtSun2.TextChanged
    Try
        Dim One As Integer
        Dim two As Integer
        Dim three As Integer
        Dim four As Integer
        Dim five As Integer
        Dim six As Integer
        Dim seven As Integer
        If CDbl(txtMon2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtTues2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtWed2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtThurs2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtFri2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtSat2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf CDbl(txtSun2.Text) > 24 Then
            MsgBox(Title:="Error", Prompt:="There not more than 24 hours a day.")
        ElseIf String.IsNullOrEmpty(txtMon2.Text) Then
            One = CInt(0)
            two = CInt(0)
            three = CInt(0)
            four = CInt(0)
            five = CInt(0)
            six = CInt(0)
            seven = CInt(0)
        ElseIf Not IsNumeric(txtMon2.Text) Then
            One = CInt(0)
            two = CInt(0)
            three = CInt(0)
            four = CInt(0)
            five = CInt(0)
            six = CInt(0)
            seven = CInt(0)
        Else
            One = CInt(Convert.ToInt64(txtMon2.Text))
            two = CInt(Convert.ToInt64(txtTues2.Text))
            three = CInt(Convert.ToInt64(txtWed2.Text))
            four = CInt(Convert.ToInt64(txtThurs2.Text))
            five = CInt(Convert.ToInt64(txtFri2.Text))
            six = CInt(Convert.ToInt64(txtSat2.Text))
            seven = CInt(Convert.ToInt64(txtSun2.Text))
            txtTot2.Text = CStr(Math.Round(One + two + three + four + five + six + seven))
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您确实应该使用Integer.TryParse(...)Double.TryParse(...)将这些字符串转换为数字。

示例:

Dim One As Integer = 0
If Not Integer.TryParse(txtMon1.Text, One) Then
  MessageBox.Show("Invalid Entry")
End If

这条线没什么意义:

One = CInt(Convert.ToInt64(txtMon2.Text))

如果您只是阅读它,它会说“我正在转换为整数转换为整数64这个字符串”。

在使用CInt(...)之前,您必须验证字符串内容是否可转换为数字。这就是TryParse(...)适合你的地方。

答案 1 :(得分:0)

在检查值是否为>之前,您需要检查文本框是否为空或为空。 24.

答案 2 :(得分:0)

干净简单的事情怎么样:

       Dim hours() As Integer = {txtMon1.Text.Trim.Length, txtTue1.Text.Trim.Length, txtWed1.Text.Trim.Length, txtThurs1.Text.Trim.Length, txtFri1.Text.Trim.Length, txtSat1.Text.Trim.Length, txtSun1.Text.Trim.Length}
    If hours.Min > 0 Then
        Dim hoursval() As Integer = {Convert.ToInt16(txtMon1.Text), Convert.ToInt16(txtTue1.Text), Convert.ToInt16(txtWed1.Text), Convert.ToInt16(txtThurs1.Text), Convert.ToInt16(txtFri1.Text), Convert.ToInt16(txtSat1.Text), Convert.ToInt16(txtSun1.Text)}
        If hoursval.Max > 24 Then
            MsgBox("There cannot be more than 24 hours in a day", MsgBoxStyle.OkOnly)
        End If
    End If

未经测试,但应该可以使用!

我忘了添加 - 这是除了使用其他人建议的tryparse之外。检查空字符串是用户常用的事情,这可以解决这个问题,而不需要很多开销。