2015-05-13 50 views
-2

編程新手。這是我高級計算器的Visual Basic代碼。我不知道如何解決這個問題,我想我必須將textbox1轉換爲整數,因爲它是字符串,但我不知道如何去做。謝謝您的幫助!Microsoft.VisualBasic.dll計算器中出現'System.InvalidCastException'

Option Explicit On 
Public Class Form1 
Dim op, FirstNumber As Integer 
Dim K As Double 
Dim I As Double 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "1" 
    Else 
     TextBox1.Text = TextBox1.Text & "1" 
    End If 

End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "2" 
    Else 
     TextBox1.Text = TextBox1.Text & "2" 
    End If 
End Sub 

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "3" 
    Else 
     TextBox1.Text = TextBox1.Text & "3" 
    End If 
End Sub 

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "4" 
    Else 
     TextBox1.Text = TextBox1.Text & "4" 
    End If 
End Sub 

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "5" 
    Else 
     TextBox1.Text = TextBox1.Text & "5" 
    End If 
End Sub 

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "6" 
    Else 
     TextBox1.Text = TextBox1.Text & "6" 
    End If 
End Sub 

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "7" 
    Else 
     TextBox1.Text = TextBox1.Text & "7" 
    End If 
End Sub 

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "8" 
    Else 
     TextBox1.Text = TextBox1.Text & "8" 
    End If 
End Sub 

Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "9" 
    Else 
     TextBox1.Text = TextBox1.Text & "9" 
    End If 
End Sub 

Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "0" 
    Else 
     TextBox1.Text = TextBox1.Text & "0" 
    End If 
End Sub 

Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click 
    TextBox1.Text = "0" 
End Sub 

Private Sub DecimalButton_Click(sender As Object, e As EventArgs) Handles DecimalButton.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "." 
    Else 
     TextBox1.Text = TextBox1.Text & "." 
    End If 
End Sub 

Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click 
    op = 1 
    FirstNumber = TextBox1.Text 
    TextBox1.Text = "" 
End Sub 

Private Sub SubtractButton_Click(sender As Object, e As EventArgs) Handles SubtractButton.Click 
    op = 2 
    FirstNumber = TextBox1.Text 
    TextBox1.Text = "" 
End Sub 

Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) Handles MultiplyButton.Click 
    op = 3 
    FirstNumber = TextBox1.Text 
    TextBox1.Text = "" 
End Sub 

Private Sub DivideButton_Click(sender As Object, e As EventArgs) Handles DivideButton.Click 
    op = 4 
    FirstNumber = TextBox1.Text 
    TextBox1.Text = "" 
End Sub 

Private Sub EqualButton_Click(sender As Object, e As EventArgs) Handles EqualButton.Click 
    If op = 1 Then 
     TextBox1.Text = Val(FirstNumber) + Val(TextBox1.Text) 
    ElseIf op = 2 Then 
     TextBox1.Text = Val(FirstNumber) - Val(TextBox1.Text) 
    ElseIf op = 3 Then 
     TextBox1.Text = Val(FirstNumber) * Val(TextBox1.Text) 
    ElseIf op = 4 Then 
     TextBox1.Text = Val(FirstNumber)/Val(TextBox1.Text) 
    ElseIf op = 5 Then 
     TextBox1.Text = Val(FirstNumber) * Val(FirstNumber) 
    ElseIf op = 6 Then 
     TextBox1.Text = Math.Sqrt(Val(FirstNumber)) 
    ElseIf op = 7 Then 
     TextBox1.Text = Val(FirstNumber)^(1/3) 
    ElseIf op = 8 Then 
     TextBox1.Text = Val(FirstNumber) * Val(FirstNumber) * Val(FirstNumber) 
    ElseIf op = 9 Then 
     TextBox1.Text = Math.Tan(Val(FirstNumber)) 
    ElseIf op = 10 Then 
     TextBox1.Text = Math.Cos(Val(FirstNumber)) 
    ElseIf op = 11 Then 
     TextBox1.Text = Math.Sin(Val(FirstNumber)) 
    End If 
End Sub 

Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click 
    Me.Close() 
End Sub 

Private Sub SQButton_Click(sender As Object, e As EventArgs) Handles SQButton.Click 
    op = 5 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub SQRButton_Click(sender As Object, e As EventArgs) Handles SQRButton.Click 
    op = 6 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub Sqr3Button_Click(sender As Object, e As EventArgs) Handles Sqr3Button.Click 
    op = 7 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub CubeButton_Click(sender As Object, e As EventArgs) Handles CubeButton.Click 
    op = 8 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub TanButton_Click(sender As Object, e As EventArgs) Handles TanButton.Click 
    op = 9 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub CosButton_Click(sender As Object, e As EventArgs) Handles CosButton.Click 
    op = 10 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub SinButton_Click(sender As Object, e As EventArgs) Handles SinButton.Click 
    op = 11 
    FirstNumber = TextBox1.Text 
End Sub 

Private Sub PiButton_Click(sender As Object, e As EventArgs) Handles PiButton.Click 
    If TextBox1.Text = "0" Then 
     TextBox1.Text = "3.14159265359" 
    Else 
     TextBox1.Text = TextBox1.Text & "3.14159265359" 
    End If 
End Sub 

Private Sub BackSpaceButton_Click(sender As Object, e As EventArgs) Handles BackSpaceButton.Click 
    TextBox1.Focus() 
    SendKeys.Send("{BackSpace}") 
End Sub 
End Class 
+0

你可以做一個[最小完整可驗證示例](http://stackoverflow.com/help/mcve),產生相同的錯誤? – Cecilia

+1

將「Option Strict On」添加到代碼中(或者在「項目屬性」中將其打開)並開始修復錯誤。您還應該使用'Convert.ToInt32'或'CInt'來代替'Val'。代碼中有許多類型轉換。具體發生的地方也是有用的。 – Plutonix

+0

除了其他評論之外,最好還是使用'Double'來進行計算,特別是如果你打算使用像PI這樣的值。 –

回答

0

在一些最好是使用雙類型,而不是整數類型,特別是因爲你有使用小數點(克里斯提到)

爲了簡單的子程序,我改變了你的整數類型到單 如果你得到一個溢出錯誤,然後改變它的雙重而不是單一

至於變化,你可以在下面找到它們。

Dim op as integer 
Dim FirstNumber As double 
Dim Result As Double 


Private Sub EqualButton_Click(sender As Object, e As EventArgs) Handles EqualButton.Click 
    If op = 1 Then 
    Result = Cdbl(FirstNumber) + Cdbl(TextBox1.Text) 
    TextBox1.Text = Str(result) 
    ElseIf op = 2 Then 
    Result = Cdbl(FirstNumber) - Cdbl(TextBox1.Text) 
    TextBox1.Text = Str(result) 
    ElseIf op = 3 Then 
    Result = Cdbl(FirstNumber) * Cdbl(TextBox1.Text) 
    TextBox1.Text = Str(result) 
    ElseIf op = 4 Then 
    Result = Cdbl(FirstNumber)/Cdbl(TextBox1.Text) 
    TextBox1.Text = Str(result) 
    ElseIf op = 5 Then 
    Result = Cdbl(FirstNumber) * Cdbl(FirstNumber) 
    TextBox1.Text = Str(result) 
    ElseIf op = 6 Then 
    Result = Math.Sqrt(Cdbl(FirstNumber)) 
    TextBox1.Text = Str(result) 
    ElseIf op = 7 Then 
    Result = Cdbl(FirstNumber)^(1/3) 
    TextBox1.Text = Str(result) 
    ElseIf op = 8 Then 
    Result = Cdbl(FirstNumber) * Cdbl(FirstNumber) * Cdbl(FirstNumber) 
    TextBox1.Text = Str(result) 
    ElseIf op = 9 Then 
    Result = Math.Tan(Cdbl(FirstNumber)) 
    TextBox1.Text = Str(result) 
    ElseIf op = 10 Then 
    Result = Math.Cos(Cdbl(FirstNumber)) 
    TextBox1.Text = Str(result) 
    ElseIf op = 11 Then 
    Result = Math.Sin(Cdbl(FirstNumber)) 
    TextBox1.Text = Str(result) 
    End If 
End Sub 

Private Sub SQButton_Click(sender As Object, e As EventArgs) Handles SQButton.Click 
    op = 5 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub SQRButton_Click(sender As Object, e As EventArgs) Handles SQRButton.Click 
    op = 6 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub Sqr3Button_Click(sender As Object, e As EventArgs) Handles Sqr3Button.Click 
    op = 7 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub CubeButton_Click(sender As Object, e As EventArgs) Handles CubeButton.Click 
    op = 8 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub TanButton_Click(sender As Object, e As EventArgs) Handles TanButton.Click 
    op = 9 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub CosButton_Click(sender As Object, e As EventArgs) Handles CosButton.Click 
    op = 10 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub SinButton_Click(sender As Object, e As EventArgs) Handles SinButton.Click 
    op = 11 
    FirstNumber = Cdbl(TextBox1.Text) 
End Sub 

Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click 
    op = 1 
    FirstNumber = Cdbl(TextBox1.Text) 
    TextBox1.Text = "" 
End Sub 

Private Sub SubtractButton_Click(sender As Object, e As EventArgs) Handles SubtractButton.Click 
    op = 2 
    FirstNumber = Cdbl(TextBox1.Text) 
    TextBox1.Text = "" 
End Sub 

Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) Handles MultiplyButton.Click 
    op = 3 
    FirstNumber = Cdbl(TextBox1.Text) 
    TextBox1.Text = "" 
End Sub 

Private Sub DivideButton_Click(sender As Object, e As EventArgs) Handles DivideButton.Click 
    op = 4 
    FirstNumber = Cdbl(TextBox1.Text) 
    TextBox1.Text = "" 
End Sub