-1

错误图像

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

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

我究竟做错了什么?

4

3 回答 3

1

您确实应该使用Integer.TryParse(...)orDouble.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(...)适合你的地方。

于 2012-08-11T14:57:57.373 回答
0

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

于 2012-08-11T14:55:42.710 回答
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 之外。检查空白字符串是用户经常做的事情,这会在没有太多开销的情况下解决这个问题。

于 2012-08-11T19:55:00.363 回答