i have to make a calculator in visual basic, it's slightly more complex one, using a combo box/procedures/functions. Anyway, i need to create a user defined function to make sure both input numbers (of the the calculation) are in fact numbers, the isnumeric function. both numbers have to be checked in the same function, and i must return a msgbox (as part of the function) displaying an error and return a value indicating there was a problem. it always crashes when i test it - entering a letter instead of a number into either of the textboxes. here's my code so far: the first function is the function that's malfunctioning. any help would be great
Code:
Public Class Form1
Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
If Not IsNumeric(num1) Then
MsgBox("Input must be a number.")
ElseIf Not IsNumeric(num2) Then
MsgBox("Input must be a number.")
Return False
End If
End Function
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim num1, num2 As Double
num1 = CStr(txtNum1.Text)
num2 = CStr(txtNum2.Text)
If cmbOperation.Text = "Addition" Then
checknumber(num1, num2)
'calculate result
addition(num1, num2)
End If
If cmbOperation.Text = "Subtraction" Then
'calculate result
subtraction(num1, num2)
End If
If cmbOperation.Text = "Multiplication" Then
'calculate result
multiplication(num1, num2)
End If
If cmbOperation.Text = "Division" Then
'calculate result
division(num1, num2)
End If
If cmbOperation.Text = "Mod" Then
'calculate result
modular(num1, num2)
End If
If cmbOperation.Text = "Exponentiation" Then
'calculate result
exponentiate(num1, num2)
End If
'validate operation
If cmbOperation.Text = "" Then
MsgBox("no operation is selected.")
End If
End Sub
Sub addition(ByVal num1 As Double, ByVal num2 As Double)
checknumber(num1, num2)
txtResult.Text = (num1 + num2)
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub subtraction(ByVal num1 As Double, ByVal num2 As Double)
txtResult.Text = num1 - num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub multiplication(ByVal num1 As Double, ByVal num2 As Double)
txtResult.Text = num1 * num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub division(ByVal num1 As Double, ByVal num2 As Integer)
'validate input
If num2 = 0 Then
MsgBox("Number 2 cannot be 0.")
Exit Sub
End If
txtResult.Text = num1 / num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub modular(ByVal num1 As Double, ByVal num2 As Double)
'validate input
If num2 = 0 Then
MsgBox("Number 2 cannot be 0.")
Exit Sub
End If
txtResult.Text = num1 Mod num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub exponentiate(ByVal num1 As Double, ByVal num2 As Double)
'validate input
If num1 = 0 And num2 < 0 Then
MsgBox("input is invalid.")
Exit Sub
End If
txtResult.Text = num1 ^ num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstResults.Items.Clear()
txtNum1.Text = ""
txtNum2.Text = ""
txtRound.Text = ""
txtResult.Text = ""
cmbOperation.Text = ""
radRound.Checked = False
End Sub
Private Sub radRound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRound.CheckedChanged
'disable rounding option unless user wants to round
If radRound.Checked = True Then
'allow user to round
txtRound.Enabled = True
Else
'do not allow rounding
txtRound.Enabled = False
End If
End Sub
Private Sub lstResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResults.SelectedIndexChanged
If lstResults.SelectedIndex >= 0 Then
If MsgBox("Use this number in the calculation?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
txtNum1.Text = lstResults.Text
End If
End If
If lstResults.SelectedIndex = -1 Then
MsgBox("no number is selected.")
Exit Sub
End If
End Sub
End Class
Comment