Option Explicit On
Imports System.Math
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Public Class frmCalculator
Dim HasResult As Boolean = False
Dim FirstNumber As Single
Dim SecondNumber As Single
Dim Arthemetic As String
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Sets the values to perform a addition equation & calls equation function
FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "+"
Call EquationText()
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
'Sets the values to perform a subtraction equation & calls equation function
FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "-"
Call EquationText()
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
'Sets the values to perform a multiply equation & calls equation function
FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "X"
Call EquationText()
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
'Sets the values to perform a division equation & calls equation function
FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "/"
Call EquationText()
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
'Sets the second number and performs the four standard equations addition, subtraction, multiplication and division
SecondNumber = Val(lblResult.Text)
Select Case Arthemetic
Case "+"
lblResult.Text = FirstNumber + SecondNumber
Case "-"
lblResult.Text = FirstNumber - SecondNumber
Case "X"
lblResult.Text = FirstNumber * SecondNumber
Case "/"
lblResult.Text = FirstNumber / SecondNumber
If SecondNumber = 0 Then
lblResult.Text = "Cannot Divide By Zero"
Exit Sub
End If
End Select
lblEquation.Text = "0"
HasResult = True
End Sub
Private Sub btnDecimalPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimalPlace.Click
'Inserts a decimal place and checks to make sure only one decimal place can be placed in each number
If lblResult.Text.IndexOf(".") >= 0 Then
lblResult.Text = lblResult.Text
Else
lblResult.Text = lblResult.Text & "."
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clears all current values in various functions
lblResult.Text = "0"
lblEquation.Text = "0"
FirstNumber = 0
SecondNumber = 0
Arthemetic = ""
lblEquation.Text = "0"
End Sub
Private Sub btnClearError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearError.Click
'Clears only the current number in the equation so number can be updated without clearing the whole equation
lblResult.Text = "0"
If Arthemetic > "" Then
SecondNumber = 0
Else
FirstNumber = 0
End If
End Sub
Private Sub Digit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
'Controls the click event for keyboard input
If HasResult Then btnClear.PerformClick() : HasResult = False
If lblResult.Text = "0" Then
lblResult.Text = sender.text
Else
lblResult.Text &= sender.text
End If
End Sub
Private Sub frmCalculator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'Sets the keyboard values to allow the user to enter data direct from the keyboard
Select Case e.KeyCode
Case Keys.Escape : btnClear_Click(btnClear, e)
Case Keys.Back : If lblResult.Text.Length > 0 Then lblResult.Text = lblResult.Text.Remove(lblResult.Text.Length - 1, 1) Else lblResult.Text = 0 : FirstNumber = CSng(lblResult.Text)
'Sets the values to perform a equation & calls equation function
Case Keys.Add : Arthemetic = "+" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
Case Keys.Subtract : Arthemetic = "-" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
Case Keys.Divide : Arthemetic = "/" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
Case Keys.Multiply : Arthemetic = "X" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
Case Keys.Enter : btnEquals.PerformClick() 'btnEquals_Click(btnEquals, e)
'Captures keyboard input for each number
Case Keys.Decimal : btnDecimalPlace.PerformClick()
Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)
Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)
End Select
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
'Exit the program
Me.Close()
End Sub
Private Sub BasicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BasicToolStripMenuItem.Click
'Resizes the form to display the basic calculator
Me.Size = New System.Drawing.Size(245, 366)
End Sub
Private Sub frmCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Resizes the form to display the basic calculator as default and display the square root icon on the button
Me.Size = New System.Drawing.Size(254, 366)
btnSquareRoot.Text = Char.ConvertFromUtf32(Integer.Parse("221A", Globalization.NumberStyles.HexNumber))
End Sub
Private Sub AdvancedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdvancedToolStripMenuItem.Click
'Resizes the form to display the Advanced calculator
Me.Size = New System.Drawing.Size(378, 366)
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
'Displays the About screen
SplashScreen1.Show()
End Sub
Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
'Performs the square root calculation
If lblResult.Text <= 0 Then
MsgBox("Cannot Find Square Root Of Negative Number", MsgBoxStyle.Information)
lblResult.Text = 0
Else
lblResult.Text = CStr(Sqrt(Val(lblResult.Text)))
End If
HasResult = True
End Sub
Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
'Performs the Percentage Calculation
lblResult.Text = FirstNumber * Val(lblResult.Text) / 100
End Sub
Private Sub EquationText()
' Displays the current equation in the equation label
lblEquation.Text = Str(FirstNumber) + " " + Arthemetic
End Sub
End Class