好了,上面的鏈接會顯示您獲得無效轉換異常,但我有一些文本框,當數字輸入,它們都自動總結,並顯示在一個單獨的盒子總和。這是我的代碼爲文本框,總和顯示在:
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
我在做什麼錯?
感謝您的答覆。我嘗試交換代碼,但錯誤仍然出現。你能想到別的嗎?另外,爲什麼我不斷得到消極代表? – Shinobi1173 2012-08-11 15:03:26
圖像中的錯誤說,你正在試圖解析一個空字符串(「」),以及明顯不分析。如果你先檢查null或空,這不應該發生。您還應該檢查以確保該字符串本質上是數字。另一個答案提到,你應該使用TryParse(),如果它是可解析的,它將返回true或false。 – brianestey 2012-08-11 15:15:31