This document contains the code for a basic calculator application with a text box for input and output and buttons for numbers and operators.
The code defines event handlers for the number and operator buttons that append numbers to the text box or set the text box to a number depending on if it is already 0.
The operator buttons also store the first number, set the text box to 0, and set flags to indicate an operation is selected.
The equals button handler performs the calculation based on the stored operation and numbers, handles division by 0 errors, and resets the operation flags.
This document contains the code for a basic calculator application with a text box for input and output and buttons for numbers and operators.
The code defines event handlers for the number and operator buttons that append numbers to the text box or set the text box to a number depending on if it is already 0.
The operator buttons also store the first number, set the text box to 0, and set flags to indicate an operation is selected.
The equals button handler performs the calculation based on the stored operation and numbers, handles division by 0 errors, and resets the operation flags.
Dim snum As Decimal TextBox1.Text += "5" Dim operation As Integer Else Dim Operator_selector As Boolean = False TextBox1.Text = "5" End If Private Sub Label1_Click(sender As Object, e As End Sub EventArgs) Private Sub Button7_Click(sender As Object, e As End Sub EventArgs) Handles Button7.Click If TextBox1.Text <> "0" Then Private Sub TextBox1_TextChanged(sender As Object, e As TextBox1.Text += "6" EventArgs) Handles TextBox1.TextChanged Else TextBox1.Text = "6" End Sub End If End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click End Sub If TextBox1.Text <> "0" Then TextBox1.Text += "7" Private Sub Button2_Click(sender As Object, e As Else EventArgs) Handles Button2.Click TextBox1.Text = "7" If TextBox1.Text <> "0" Then End If TextBox1.Text += "1" End Sub Else TextBox1.Text = "1" Private Sub Button9_Click(sender As Object, e As End If EventArgs) Handles Button9.Click If TextBox1.Text <> "0" Then End Sub TextBox1.Text += "8" Else Private Sub Button3_Click(sender As Object, e As TextBox1.Text = "8" EventArgs) Handles Button3.Click End If If TextBox1.Text <> "0" Then End Sub TextBox1.Text += "2" Else Private Sub Button10_Click(sender As Object, e As TextBox1.Text = "2" EventArgs) Handles Button10.Click End If If TextBox1.Text <> "0" Then End Sub TextBox1.Text += "9" Else Private Sub Button4_Click(sender As Object, e As TextBox1.Text = "9" EventArgs) Handles Button4.Click End If If TextBox1.Text <> "0" Then End Sub TextBox1.Text += "3" Else Private Sub Button1_Click(sender As Object, e As TextBox1.Text = "3" EventArgs) Handles Button1.Click End If If TextBox1.Text <> "0" Then End Sub TextBox1.Text += "0" End If Private Sub Button5_Click(sender As Object, e As End Sub EventArgs) Handles Button5.Click If TextBox1.Text <> "0" Then Private Sub Button17_Click(sender As Object, e As TextBox1.Text += "4" EventArgs) Handles Button17.Click Else TextBox1.Text = "0" TextBox1.Text = "4" End If End Sub End Sub Private Sub Button16_Click(sender As Object, e As Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button16.Click EventArgs) Handles Button6.Click If Not (TextBox1.Text.Contains(".")) Then TextBox1.Text += "."
End If End Sub
Private Sub Button11_Click(sender As Object, e As
EventArgs) Handles Button11.Click fnum = TextBox1.Text TextBox1.Text = "0" Operator_selector = True operation = 1 ' = + End Sub
Private Sub Button12_Click(sender As Object, e As
EventArgs) Handles Button12.Click fnum = TextBox1.Text TextBox1.Text = "0" Operator_selector = True operation = 2 ' = + End Sub
Private Sub Button13_Click(sender As Object, e As
EventArgs) Handles Button13.Click fnum = TextBox1.Text TextBox1.Text = "0" Operator_selector = True operation = 3 ' = * End Sub
Private Sub Button14_Click(sender As Object, e As
EventArgs) Handles Button14.Click fnum = TextBox1.Text TextBox1.Text = "0" Operator_selector = True operation = 4 ' = / End Sub
Private Sub Button15_Click(sender As Object, e As
EventArgs) Handles Button15.Click If Operator_selector = True Then snum = TextBox1.Text If operation = 1 Then TextBox1.Text = fnum + snum ElseIf operation = 2 Then TextBox1.Text = fnum - snum ElseIf operation = 3 Then TextBox1.Text = fnum * snum Else If snum = 0 Then TextBox1.Text = "SYNTAX ERROR" Else TextBox1.Text = fnum / snum End If Operator_selector = False End If End If End Sub Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click If TextBox1.Text <> "0" Then TextBox1.Text += "-" Else TextBox1.Text = "-" End If End Sub End Class