Microsoft.VisualBasic.dll计算器中发生'System.InvalidCastException'

时间:2015-05-13 19:17:20

标签: vb.net calculator

我是编程新手。这是我的高级计算器的Visual Basic代码。我不知道如何解决问题,我想我必须将textbox1转换为interger,因为它是字符串但我不知道如何。谢谢你的帮助!

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

1 个答案:

答案 0 :(得分:0)

在某些子程序中,最好使用DOUBLE类型而不是INTEGER类型,尤其是因为您使用了小数点(正如Chris所提到的)

为简单起见,我将整数类型更改为SINGLE 如果出现溢出错误,则将其更改为double而不是单个

至于更改,您可以在下面找到它们。

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