-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(...)orを使用する必要があります。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(...)ために働くところです。

于 2012-08-11T14:57:57.373 に答える
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 に答える
0

値が > 24 であるかどうかを確認する前に、テキスト ボックスが null または空であるかどうかを確認する必要があります。

于 2012-08-11T14:55:42.710 に答える