-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
你可以做一個[最小完整可驗證示例](http://stackoverflow.com/help/mcve),產生相同的錯誤? – Cecilia
將「Option Strict On」添加到代碼中(或者在「項目屬性」中將其打開)並開始修復錯誤。您還應該使用'Convert.ToInt32'或'CInt'來代替'Val'。代碼中有許多類型轉換。具體發生的地方也是有用的。 – Plutonix
除了其他評論之外,最好還是使用'Double'來進行計算,特別是如果你打算使用像PI這樣的值。 –