Skip to content

Instantly share code, notes, and snippets.

@apremalal
Created April 20, 2013 16:50
Show Gist options
  • Save apremalal/5426606 to your computer and use it in GitHub Desktop.
Save apremalal/5426606 to your computer and use it in GitHub Desktop.
Public Class Form1
'Global variables
Public value1 As Double
Public value2 As Double
Public previousOperator As Char
Public currentOperator As Char
Public operated As Boolean
Public answerPresented As Boolean
Public buttonClicked As String 'keep trak of the currently clicked button
'Initializing variables and user interface
Public Sub New()
InitializeComponent()
value1 = 0
value2 = 0
operated = False
answerPresented = False
previousOperator = "N"
currentOperator = "N"
End Sub
Private Sub numButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click, Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
ShowValue(sender)
End Sub
'Clear the text box
Private Sub ShowValue(ByVal button As Button)
buttonClicked = button.Text 'update the currently clicked button
If answerPresented Then
Double.TryParse(Me.TextBox1.Text, value1)
Me.TextBox1.Text = button.Text
answerPresented = False
Else
Me.TextBox1.Text = Val(Me.TextBox1.Text & button.Text)
End If
End Sub
Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
ClearAll()
End Sub
Private Sub optBtns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlus.Click, ButtonMinus.Click, ButtonMultiply.Click, ButtonDivide.Click
If isPreviouslyClicked(sender) Then
Return
End If
If currentOperator <> "N" Then
previousOperator = currentOperator
currentOperator = DirectCast(sender, Button).Text
Double.TryParse(Me.TextBox1.Text, value2)
calculate(value1, value2, previousOperator)
clearTextBox_UpdateDisplay(currentOperator)
Me.TextBox1.Text = value1
answerPresented = True
Return
End If
Double.TryParse(Me.TextBox1.Text, value1)
If Me.TextBox1.Text <> "" And value1 <> 0 Then
currentOperator = DirectCast(sender, Button).Text
clearTextBox_UpdateDisplay(currentOperator)
End If
End Sub
'Calclation of 4 major mathematics operators
Public Sub calculate(ByRef part1 As Double, ByVal part2 As Double, ByVal [operator] As String)
Select Case [operator]
Case "+"
part1 += part2
Case "-"
part1 -= part2
Case "/"
part1 /= part2
Case "*"
part1 *= part2
End Select
value2 = 0
End Sub
Private Sub ButtonRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRoot.Click
If isPreviouslyClicked(sender) Then
Return
End If
If Me.TextBox1.Text <> "" Then
Double.TryParse(Me.TextBox1.Text, value2)
Me.TextBox1.Text = Math.Sqrt(value2)
End If
End Sub
Private Sub ButtonOneoverX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOneoverX.Click
If isPreviouslyClicked(sender) Then
Return
End If
If Me.TextBox1.Text <> "" Then
Double.TryParse(Me.TextBox1.Text, value2)
Me.TextBox1.Text = 1 / value2
answerPresented = True
End If
End Sub
Private Sub ButtonDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDot.Click
If isPreviouslyClicked(sender) Or Me.TextBox1.Text.Contains(".") Then
Return
Else
Me.TextBox1.Text = Me.TextBox1.Text + "."
End If
End Sub
'this method clear the text box and updates the results on the lable
Private Sub clearTextBox_UpdateDisplay(ByVal currentOperator As Char)
Dim s As String = Me.Label1.Text.ToString()
If s.Length = 0 And Me.TextBox1.Text <> "" Then
Me.Label1.Text = Me.Label1.Text & Me.TextBox1.Text & " " & currentOperator
Me.TextBox1.Text = ""
Return
End If
If Me.TextBox1.Text <> "" Then
Me.Label1.Text = Me.Label1.Text & Me.TextBox1.Text & " " & currentOperator
Me.TextBox1.Text = ""
End If
End Sub
Private Sub ButtonEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEquals.Click
If isPreviouslyClicked(sender) Then
Return
End If
If currentOperator <> "N" Then
previousOperator = currentOperator
Double.TryParse(Me.TextBox1.Text, value2)
calculate(value1, value2, previousOperator)
Me.Label1.Text = ""
Me.TextBox1.Text = value1
answerPresented = True
Else
Me.Label1.Text = ""
Double.TryParse(Me.TextBox1.Text, value1)
End If
resetVariables()
End Sub
'Reset all the global variables and clear text
Private Sub ClearAll()
Me.TextBox1.Text = ""
Me.Label1.Text = ""
operated = False
answerPresented = False
value1 = 0
value2 = 0
currentOperator = "N"
previousOperator = "N"
buttonClicked = ""
End Sub
'Rest application variables without clearing the displayed output
Private Sub resetVariables()
operated = False
answerPresented = False
value1 = 0
value2 = 0
currentOperator = "N"
previousOperator = "N"
buttonClicked = ""
End Sub
'check whether the button clicked is the same as previous
'if yes return true
Private Function isPreviouslyClicked(ByVal sender As Button) As Boolean
If buttonClicked = sender.Text Then
buttonClicked = sender.Text 'update the currently clicked button
Return True
Else
buttonClicked = sender.Text 'update the currently clicked button
Return False
End If
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment