Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

VB.Net, Calculator project that has 2 Text Boxes for number entry, a drop down l

ID: 3753428 • Letter: V

Question

VB.Net, Calculator project that has 2 Text Boxes for number entry, a drop down list, "OperatorList", for math operator, and a Calculate button.

I need to display a message on the "Result Label", not a message box, stating "You cannot divide by zero". My code is below but the message won't display. Please help.

Protected Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
If ValueBox1.Text.Length > 0 AndAlso ValueBox2.Text.Length > 0 Then

Dim result As Double = 0
Dim value1 As Double = Convert.ToDouble(ValueBox1.Text)
Dim value2 As Double = Convert.ToDouble(ValueBox2.Text)

Select Case OperatorList.SelectedValue
Case "+"
result = value1 + value2
Case "-"
result = value1 - value2
Case "*"
result = value1 * value2
Case "/"
result = value1 / value2
End Select
If OperatorList.SelectedValue = "/" Then
If value2 <> 0 Then
ResultLable.Text = "You cannot divide by Zero"
Else
result = value1 / value2
End If
End If
ResultLable.Text = result.ToString()
Else
ResultLable.Text = String.Empty
End If
End Sub

Explanation / Answer

Try the below program , '/' is used in two condition and also it is compared using <>(not equal to operator) . So I modify the below code. Give a try

Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click

If ValueBox1.Text.Length > 0 And ValueBox2.Text.Length > 0 Then

Dim result As Double = 0
Dim value1 As Double = Convert.ToDouble(ValueBox1.Text)
Dim value2 As Double = Convert.ToDouble(ValueBox2.Text)

Select Case OperatorList.SelectedValue
Case "+"
result = value1 + value2
Case "-"
result = value1 - value2
Case "*"
result = value1 * value2
End Select
If OperatorList.SelectedValue = "/" Then
If value2 == 0 Then
ResultLable.Text = "You cannot divide by Zero"
Else
result = value1 / value2
End If
End If
ResultLable.Text = result.ToString()
Else
ResultLable.Text = String.Empty
End If
End Sub