Question
To clearify I need Visual Basic code not C# code. Exercise 7-2 Add data validation to the simple calculator In this exercise, youll add data
To clearify I need Visual Basic code not C# code.
Exercise 7-2 Add data validation to the simple calculator
In this exercise, youll add data validation to the Simple Calculator form of exercise 7-1.
1. Open the SimpleCalculator project in the Assignment Exercises\Chapter 07\SimpleCalculator With Data Validation directory.
2. Code methods named IsPresent, IsDecimal, and IsWithinRange that work like the methods described in chapter 7 of the book.
3. Code a method named IsOperator that checks that the text box thats passed to it contains a value of +, -, *, or /.
4. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.
5. Delete all of the catch blocks from the TryCatch statement in the btnCalculate_Click event handler except for the one that catches any exception. Then, add code to this event handler that performs the calculation and displays the result only if the values of the text boxes are valid.
6. Test the application to be sure that all the data is validated properly.
Here is my current Visual Basic code:
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click Try Dim operand1 As Decimal = CDec(txtOperand1.Text) Dim operator1 As String = txtOperator.Text Dim operand2 As Decimal = CDec(txtOperand2.Text) Dim result As Decimal = CalculationResult(operand1, operator1, operand2)
result = Math.Round(result, 4) txtResult.Text = result.ToString Catch ex As InvalidCastException MessageBox.Show("Invalid numeric format. Please check all entries.", "Entry Error") Catch ex As OverflowException MessageBox.Show("Overflow error. Please enter smaller values.", "Entry Error") Catch ex As DivideByZeroException MessageBox.Show("Divide-by-zero error. Please enter a non-zero value for operand 2.", "Entry Error") Catch ex As Exception MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.GetType.ToString & vbCrLf & vbCrLf & ex.StackTrace, "Exception") End Try End Sub
Private Function CalculationResult(operand1 As Decimal, operator1 As String, operand2 As Decimal) As Decimal If operator1 = "+" Then CalculationResult = operand1 + operand2 ElseIf operator1 = "-" Then CalculationResult = operand1 - operand2 ElseIf operator1 = "*" Then CalculationResult = operand1 * operand2 ElseIf operator1 = "/" Then CalculationResult = operand1 / operand2 End If Return CalculationResult End Function
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub
Private Sub ClearResult(sender As Object, e As EventArgs) _ Handles txtOperand1.TextChanged, txtOperator.TextChanged, txtOperand2.TextChanged txtResult.Text = "" End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started