Hello everyone!!
I have an assignment to create that wonderful mortgage calculator. I have the code working, but when i try to catch an error (No matter is it is left blank, or a letter is typed in) the program crashes. Not sure what i am doing wrong, but any in site would be great. Thanks everyone!!
Public Class MortCalc
Private Sub clearForm()
txtLoan.Text = 200000
txtInterest.Text = 5.75
txtYears.Text = 30
txtTotal.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
'New Varables
Dim txtPrincipal As Double
Dim txtRate As Double
Dim txtYears As Integer
Dim amount As Double
Dim monthlyRate As Double
Dim years As Integer
amount = CDbl(Me.txtLoan.Text)
monthlyRate = CDbl(Me.txtInterest.Text) / 100 'allows interest rate to be entered whole number'
years = CInt(Me.txtYears.Text)
If IsNumeric(txtLoan.Text) = False Then
MsgBox("Please enter a valid loan amount.")
txtLoan.Clear()
txtLoan.Focus()
Exit Sub
End If
If IsNumeric(txtInterest.Text) = False Then
MsgBox("Please enter a valid Interest Rate")
txtInterest.Clear()
txtInterest.Focus()
Exit Sub
End If
If IsNumeric(Me.txtYears.Text) = False Then
MsgBox("Please enter a valid year")
Me.txtYears.Clear()
Me.txtYears.Focus()
End If
txtPrincipal = FormatCurrency(amount)
txtRate = FormatCurrency(monthlyRate)
txtYears = FormatCurrency(years) * 12
Dim iRate As Double = monthlyRate / 12
Dim tTotal As Double = txtPrincipal * (iRate / (1 - (1 + iRate) ^ (-txtYears)))
Me.txtTotal.Text = Format(tTotal, "$#,##0.00")
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
clearForm()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
End Class