Public Class Form1
    Dim memory As Double 'память калькулятора
    Dim Operation As Integer
    Dim a As Double : Dim b As Double
    Dim n As Boolean = False
    Sub fun() 'позволяет делать расчет не только при нажатии enter, но и выч. клавиш
        If Operation = 0 Then
            a = Double.Parse(TextBox1.Text)
        Else : b = Double.Parse(TextBox1.Text)
            If Operation = 1 Then
                TextBox1.Text = (a + b)
            ElseIf Operation = 2 Then
                TextBox1.Text = (a - b)
            ElseIf Operation = 3 Then
                TextBox1.Text = (a * b)
            ElseIf Operation = 4 Then
                TextBox1.Text = a / b
            End If
            Operation = 0
            n = True
        End If
    End Sub
    Function number(ByVal x As String) As Integer
        Dim pace As Integer = 0
        Do Until x = ""
            x = x.Substring(1)
            pace += 1
        Loop
        Return pace
    End Function
    
    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        If n = False Then
            TextBox1.Text &= 1
        Else : TextBox1.Text = 1
        End If
        n = False
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        If n = False Then
            TextBox1.Text &= 0
        Else : TextBox1.Text = 0
        End If
        n = False
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If n = False Then
            TextBox1.Text &= ","
        Else : TextBox1.Text = ","
        End If
        n = False
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        If n = False Then
            TextBox1.Text &= 2
        Else : TextBox1.Text = 2
        End If
        n = False
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        If n = False Then
            TextBox1.Text &= 3
        Else : TextBox1.Text = 3
        End If
        n = False
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        If n = False Then
            TextBox1.Text &= 4
        Else : TextBox1.Text = 4
        End If
        n = False
    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        If n = False Then
            TextBox1.Text &= 5
        Else : TextBox1.Text = 5
        End If
        n = False
    End Sub

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        If n = False Then
            TextBox1.Text &= 6
        Else : TextBox1.Text = 6
        End If
        n = False
    End Sub

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
        If n = False Then
            TextBox1.Text &= 7
        Else : TextBox1.Text = 7
        End If
        n = False
    End Sub

    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        If n = False Then
            TextBox1.Text &= 8
        Else : TextBox1.Text = 8
        End If
        n = False
    End Sub
    Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click
        If n = False Then
            TextBox1.Text &= 9
        Else : TextBox1.Text = 9
        End If
        n = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        memory = 0 : memory = Double.Parse(TextBox1.Text)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        memory += Double.Parse(TextBox1.Text)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text = memory
        n = True
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

        If TextBox1.Text.Substring(0, 1) = "-" Then
            TextBox1.Text = TextBox1.Text.Substring(1)
        Else
            TextBox1.Text = "-" & TextBox1.Text
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        fun()
        Operation = 1
        n=True 
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        fun()
        Operation = 2
        n = True
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        fun()
        Operation = 3
        n = True
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        fun()
        Operation = 4
        n = True
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        b = Double.Parse(TextBox1.Text)
        If Operation = 1 Then
            TextBox1.Text = (a + b)
        ElseIf Operation = 2 Then
            TextBox1.Text = (a - b)
        ElseIf Operation = 3 Then
            TextBox1.Text = (a * b)
        ElseIf Operation = 4 Then
            TextBox1.Text = a / b
        Else : TextBox1.Text = b
        End If
        Operation = 0
        n = True
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        a = 0 : b = 0 : n = False : TextBox1.Text = "0"
        n = True
    End Sub

    Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click
        If n = True Then
            TextBox1.Text = "0"
        Else
            If number(TextBox1.Text) = 0 Then
            Else
                TextBox1.Text = TextBox1.Text.Substring(0, number(TextBox1.Text) - 1)
            End If
        End If
    End Sub
End Class
