Question: I need help with this problem in Visual Basic 2012: Write a program that allows the user to specify two numbers and then adds, subtracts,
I need help with this problem in Visual Basic 2012:
Write a program that allows the user to specify two numbers and then adds, subtracts, multiplies, or divides them when the user clicks on the appropriate button. The output should give the type of arithmetic performed and the result. Whenever one of the numbers in an input text box is changed, the output text box should be cleared. Also, if the number 0 is entered into the second text box, the division button should be disabled.
Here is my code so far. The button disabling is what's troubling me the most.
Option Strict On Public Class FrmCalculator
Private Sub TxtFirstNum_TextChanged(sender As Object, e As EventArgs) Handles TxtFirstNum.TextChanged TxtSolution.Clear() End Sub
Private Sub TxtSecondNum_TextChanged(sender As Object, e As EventArgs) Handles TxtSecondNum.TextChanged TxtSolution.Clear() End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click If (IsNumeric(TxtFirstNum.Text)) And (IsNumeric(TxtSecondNum.Text)) Then Dim num1 As Double = CDbl(TxtFirstNum.Text) Dim num2 As Double = CDbl(TxtSecondNum.Text) Dim sum As Double = CDbl(num1 + num2) TxtSolution.Text = CStr(num1 & "+" & num2 & "=" & sum) ElseIf Not (IsNumeric(TxtFirstNum.Text)) And Not (IsNumeric(TxtSecondNum.Text)) Then BtnAdd.Enabled = False End If End Sub
Private Sub BtnSubtract_Click(sender As Object, e As EventArgs) Handles BtnSubtract.Click Dim num1 As Double = CDbl(TxtFirstNum.Text) Dim num2 As Double = CDbl(TxtSecondNum.Text) Dim difference As Double = CDbl(num1 - num2) TxtSolution.Text = CStr(num1 & "-" & num2 & "=" & difference) End Sub
Private Sub BtnMultiply_Click(sender As Object, e As EventArgs) Handles BtnMultiply.Click Dim num1 As Double = CDbl(TxtFirstNum.Text) Dim num2 As Double = CDbl(TxtSecondNum.Text) Dim product As Double = CDbl(num1 * num2) TxtSolution.Text = CStr(num1 & "x" & num2 & "=" & product) End Sub
Private Sub BtnDivide_Click(sender As Object, e As EventArgs) Handles BtnDivide.Click Dim num1 As Double = CDbl(TxtFirstNum.Text) Dim num2 As Double = CDbl(TxtSecondNum.Text) Dim quotient As Double = CDbl(num1 / num2) TxtSolution.Text = CStr(num1 & "/" & num2 & "=" & quotient) If CDbl(TxtSecondNum.Text) = 0 Then BtnDivide.Enabled = False End If End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
