Here is some terrible Visual Basic code for an equal button on a Window – like, calculator.
Private Sub btnEqual_Click(sender As System.Object, e As System.EventArgs) Handles btnEqual.Click Dim result As Double 'To store the result. Select Case theOperation 'Case for which operation is being performed, determined by an enum
'''
''' enter the current number from the text box into the second number. The button
''' also puts the result into the text box/ error checks.
'''
Try
CheckEmpty(txtData.Text) 'Making sure the text box is not empty.
numbers.Number2 = CDbl(txtData.Text) 'Storing the second number.
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
Pingback: Fancy calculator | Jaykaymotors