Squareroot和Decimals的计算器代码

时间:2018-03-04 13:06:47

标签: vb.net

这是我的计算器设计图片

enter image description here

我一直在尝试使用其他代码来处理我的计算器,但它们无法正常工作,而且我需要十进制和平方根的代码。我还是编程新手。我这样做的方式很长。我听说有一些简短的方法可以做到这一点,但令人困惑

Dim Firstnum As Decimal
Dim secondnum As Decimal
Dim Operations As Integer
Dim Operator_selctor As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "1"
    Else
        TextBox1.Text = "1"
    End If
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "2"
    Else
        TextBox1.Text = "2"
    End If
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "3"
    Else
        TextBox1.Text = "3"
    End If
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "4"
    Else
        TextBox1.Text = "4"
    End If
End Sub

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

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "6"
    Else
        TextBox1.Text = "6"
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "7"
    Else
        TextBox1.Text = "7"
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "8"
    Else
        TextBox1.Text = "8"
    End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "9"
    Else
        TextBox1.Text = "9"
    End If
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    If TextBox1.Text <> "0" Then
        TextBox1.Text += "0"
    End If
End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    TextBox1.Text = "0"
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    If Not (TextBox1.Text.Contains(".")) Then
        TextBox1.Text += "."
    End If
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
    Firstnum = TextBox1.Text
    TextBox1.Text = "0"
    Operator_selctor = True
    Operations = 1 ' = +    
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
    Firstnum = TextBox1.Text
    TextBox1.Text = "0"
    Operator_selctor = True
    Operations = 2 ' - -
End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    Firstnum = TextBox1.Text
    TextBox1.Text = "0"
    Operator_selctor = True
    Operations = 3 '=x

End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    Firstnum = TextBox1.Text
    TextBox1.Text = "0"
    Operator_selctor = True
    Operations = 4 ' /
End Sub

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
    If Operator_selctor = True Then
        secondnum = TextBox1.Text
        If Operations = 1 Then
            TextBox1.Text = Firstnum + secondnum
        ElseIf Operations = 2 Then
            TextBox1.Text = Firstnum - secondnum
        ElseIf Operations = 3 Then
            TextBox1.Text = Firstnum * secondnum
        Else
            If secondnum = 0 Then
                TextBox1.Text = "Error!"
            Else
                TextBox1.Text = Firstnum / secondnum
            End If
        End If
        Operator_selctor = False
    End If
End Sub

0 个答案:

没有答案