Fancy Calculator Equal Button

Here is some terrible Visual Basic code for an equal button on a Window – like, calculator.


'''

''' Equal button to perform the entered operation on the first stored number, and
''' enter the current number from the text box into the second number. The button
''' also puts the result into the text box/ error checks.
'''

Private Sub btnEqual_Click(sender As System.Object, e As System.EventArgs) Handles btnEqual.Click
Try
CheckEmpty(txtData.Text) 'Making sure the text box is not empty.

Dim result As Double 'To store the result.
numbers.Number2 = CDbl(txtData.Text) 'Storing the second number.

Select Case theOperation 'Case for which operation is being performed, determined by an enum
Case Operations.none 'No operation has been entered, error.
Throw New Exception("You must select an operation to be performed!") 'Throw exception
Case Operations.addition 'Addition operation
Dim add As AddNumbersClass = New AddNumbersClass
result = add.operation(numbers.Number1, numbers.Number2)
Case Operations.subtraction 'Subtraction operation
Dim subtract As SubtractNumbersClass = New SubtractNumbersClass
result = subtract.operation(numbers.Number1, numbers.Number2)
Case Operations.multiplication 'Multiplication operation
Dim multiply As MultiplyNumbersClass = New MultiplyNumbersClass
result = multiply.operation(numbers.Number1, numbers.Number2)
Case Operations.division 'Division operation
If (numbers.Number2 = 0) Then 'Can't divide by zero
Throw New Exception("Cannot divide by zero!")
End If
Dim divide As DivideNumbersClass = New DivideNumbersClass
result = divide.operation(numbers.Number1, numbers.Number2)
Case Operations.power 'Power operation
Dim power As PowerNumbersClass = New PowerNumbersClass
result = power.operation(numbers.Number1, numbers.Number2)
End Select
txtData.Text = result 'Put the result into the text box
PositionCursor() 'Reposition the cursor
Catch ex As Exception 'Catch any thrown exception
MessageBox.Show(ex.ToString() & vbCrLf)
btnClear.PerformClick()
End Try
End Sub

This entry was posted in Breaking News. Bookmark the permalink.

One Response to Fancy Calculator Equal Button

  1. Pingback: Fancy calculator | Jaykaymotors

Leave a Reply