' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace DefaultNamespace
    Public Class MainForm
        Inherits Form

        Private acc As Double
        Private op As String
        Private tb As TextBox
        Private btnOne As Button
        Private btnTwo As Button
        Private btnThree As Button
        Private btnFour As Button
        Private btnFive As Button
        Private btnSix As Button
        Private btnSeven As Button
        Private btnEigth As Button
        Private btnNine As Button
        Private btnZero As Button
        Private btnPlus As Button
        Private btnMinus As Button
        Private btnMultiply As Button
        Private btnDivide As Button
        Private btnEqual As Button

        Public Shared Sub Main
            Dim fMainForm As New MainForm
            fMainForm.ShowDialog()
        End Sub

        Public Sub New()
            MyBase.New
            Me.InitializeComponent
        End Sub

        Private Sub InitializeComponent()
            acc = 0
            op = ""
            tb = New TextBox
            btnOne = New Button
            btnTwo = New Button
            btnThree = New Button
            btnFour = New Button
            btnFive = New Button
            btnSix = New Button
            btnSeven = New Button
            btnEigth = New Button
            btnNine = New Button
            btnZero = New Button
            btnPlus = New Button
            btnMinus = New Button
            btnMultiply = New Button
            btnDivide = New Button
            btnEqual = New Button
            
            SuspendLayout
            
            tb.Location = New Point(50, 50)
            tb.Size = New Size(350, 50)
            tb.Text = ""
            btnOne.Location = New Point(50, 150)
            btnOne.Size = New Size(50, 50)
            btnOne.Text = "1"
            AddHandler btnOne.Click, AddressOf ButtonClick
            btnTwo.Location = New Point(150, 150)
            btnTwo.Size = New Size(50, 50)
            btnTwo.Text = "2"
            AddHandler btnTwo.Click, AddressOf ButtonClick
            btnThree.Location = New Point(250, 150)
            btnThree.Size = New Size(50, 50)
            btnThree.Text = "3"
            AddHandler btnThree.Click, AddressOf ButtonClick
            btnFour.Location = New Point(50, 250)
            btnFour.Size = New Size(50, 50)
            btnFour.Text = "4"
            AddHandler btnFour.Click, AddressOf ButtonClick
            btnFive.Location = New Point(150, 250)
            btnFive.Size = New Size(50, 50)
            btnFive.Text = "5"
            AddHandler btnFive.Click, AddressOf ButtonClick
            btnSix.Location = New Point(250, 250)
            btnSix.Size = New Size(50, 50)
            btnSix.Text = "1"
            AddHandler btnSix.Click, AddressOf ButtonClick
            btnSeven.Location = New Point(50, 350)
            btnSeven.Size = New Size(50, 50)
            btnSeven.Text = "7"
            AddHandler btnSeven.Click, AddressOf ButtonClick
            btnEigth.Location = New Point(150, 350)
            btnEigth.Size = New Size(50, 50)
            btnEigth.Text = "8"
            AddHandler btnEigth.Click, AddressOf ButtonClick
            btnNine.Location = New Point(250, 350)
            btnNine.Size = New Size(50, 50)
            btnNine.Text = "9"
            AddHandler btnNine.Click, AddressOf ButtonClick
            btnZero.Location = New Point(50, 450)
            btnZero.Size = New Size(50, 50)
            btnZero.Text = "0"
            AddHandler btnZero.Click, AddressOf ButtonClick
            btnPlus.Location = New Point(350, 150)
            btnPlus.Size = New Size(50, 50)
            btnPlus.Text = "+"
            AddHandler btnPlus.Click, AddressOf ButtonClick
            btnMinus.Location = New Point(350, 250)
            btnMinus.Size = New Size(50, 50)
            btnMinus.Text = "-"
            AddHandler btnMinus.Click, AddressOf ButtonClick
            btnMultiply.Location = New Point(350, 350)
            btnMultiply.Size = New Size(50, 50)
            btnMultiply.Text = "*"
            AddHandler btnMultiply.Click, AddressOf ButtonClick
            btnDivide.Location = New Point(350, 450)
            btnDivide.Size = New Size(50, 50)
            btnDivide.Text = "/"
            AddHandler btnDivide.Click, AddressOf ButtonClick
            btnEqual.Location = New Point(150, 450)
            btnEqual.Size = New Size(150, 50)
            btnEqual.Text = "="
            AddHandler btnEqual.Click, AddressOf ButtonClick
            ClientSize = New Size(450, 550)
            Controls.Add(tb)
            Controls.Add(btnOne)
            Controls.Add(btnTwo)
            Controls.Add(btnThree)
            Controls.Add(btnFour)
            Controls.Add(btnFive)
            Controls.Add(btnSix)
            Controls.Add(btnSeven)
            Controls.Add(btnEigth)
            Controls.Add(btnNine)
            Controls.Add(btnZero)
            Controls.Add(btnPlus)
            Controls.Add(btnMinus)
            Controls.Add(btnMultiply)
            Controls.Add(btnDivide)
            Controls.Add(btnEqual)
            Text = "Calculator"
            
            ResumeLayout(false)
        End Sub
        Private Sub ButtonClick(sender As Object, e As EventArgs)
            If sender Is btnOne Then
                tb.Text = tb.Text & "1"
            End If          
            If sender Is btnTwo Then
                tb.Text = tb.Text & "2"
            End If          
            If sender Is btnThree Then
                tb.Text = tb.Text & "3"
            End If          
            If sender Is btnFour Then
                tb.Text = tb.Text & "4"
            End If          
            If sender Is btnFive Then
                tb.Text = tb.Text & "5"
            End If          
            If sender Is btnSix Then
                tb.Text = tb.Text & "6"
            End If          
            If sender Is btnSeven Then
                tb.Text = tb.Text & "7"
            End If          
            If sender Is btnEigth Then
                tb.Text = tb.Text & "8"
            End If          
            If sender Is btnNine Then
                tb.Text = tb.Text & "9"
            End If          
            If sender Is btnZero Then
                tb.Text = tb.Text & "0"
            End If          
            If sender Is btnPlus Then
                acc = Double.Parse(tb.Text)
                op = "+"
                tb.Text = ""
            End If          
            If sender Is btnMinus Then
                acc = Double.Parse(tb.Text)
                op = "-"
                tb.Text = ""
            End If          
            If sender Is btnMultiply Then
                acc = Double.Parse(tb.Text)
                op = "*"
                tb.Text = ""
            End If          
            If sender Is btnDivide Then
                acc = Double.Parse(tb.Text)
                op = "/"
                tb.Text = ""
            End If          
            If sender Is btnEqual Then
                If op = "+" Then
                    acc = acc + Double.Parse(tb.Text)
                End If
                If op = "-" Then
                    acc = acc - Double.Parse(tb.Text)
                End If
                If op = "*" Then
                    acc = acc * Double.Parse(tb.Text)
                End If
                If op = "/" Then
                    acc = acc / Double.Parse(tb.Text)
                End If
                tb.Text = acc.ToString
                op = ""
            End If          
        End Sub
    End Class
End Namespace
