Reputation: 23
I am programming in Visual Basic and i have a program created that is a loan calculator. I pretty much have everything correct and it works and operates almost the way i want it too.
Here is my code so far:
Public Class Form1
Private MinAmt As Decimal = 1000
Private MaxAmt As Decimal = 200000
Private Drate As Decimal
Private Loan As Decimal
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'== Load the Rate and Terms combo, then 'click Reset
For r As Decimal = 0 To 0.1475 Step 0.0025
'== Add each successive value to the combo list
cbxRate.Items.Add(Format(r, "00.00%"))
Next
For t As Byte = 12 To 120 Step 6
cbxTerm.Items.Add(Format(t, "00"))
Next
btnReset.PerformClick()
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
'== Set the defaults, and clear my prior answers
cbxRate.Text = "06.00%"
cbxTerm.Text = "36"
tbxLoan.Text = "10,000"
lblPayment.Text = ""
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
'== Validate the loan amount (number 1,000 - 200,000);
If IsNumeric(tbxLoan.Text) AndAlso tbxLoan.Text >= MinAmt AndAlso tbxLoan.Text <= MaxAmt Then
Loan = tbxLoan.Text
Else
MessageBox.Show("Error invalid value")
tbxLoan.Focus()
tbxLoan.SelectAll()
End If
'== Calculate Monthly Payment as PMT(monthly decimal rate, term in months, minus amount)
lblPayment.Text = Pmt(cbxRate.Text.Replace("%", "") / 1200, cbxTerm.Text, -tbxLoan.Text)
'== Display formatted as money: FormatCurrency(blah blah blah)
lblPayment.Text = FormatCurrency(lblPayment.Text, 2)
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
'== Confirm he means it, else cancel attempt to close.
If MessageBox.Show("Do you really want to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Else
e.Cancel = True
End If
End Sub
End Class
I tried to post a picture of my program so it could be easier, but apparently i don't have enough reputation points, but ill try to explain the best i can.
I would like to eliminate the problem of someone accidentally putting a Letter as a value in the "Enter Loan Amount" space. Right now if i were to put a letter "U" in the "Enter Loan Amount" space it would pop up an error (which is what i want), but it also crashes the program (what i don't want).
Hopefully that makes sense. Would anyone be able to point me in the right direction? I am still new to Visual Basic.
Thank you!
Upvotes: 1
Views: 668
Reputation: 30
You are still executing the rest of the code. You need to exit that block. Make use of Return statement..
Else
MessageBox.Show("Error invalid value")
tbxLoan.Focus()
tbxLoan.SelectAll()
Return
End If
Upvotes: 0
Reputation: 251
or just move the calculations to inside the If statement
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
'== Validate the loan amount (number 1,000 - 200,000);
If IsNumeric(tbxLoan.Text) AndAlso tbxLoan.Text >= MinAmt AndAlso tbxLoan.Text <= MaxAmt Then
Loan = tbxLoan.Text
'== Calculate Monthly Payment as PMT(monthly decimal rate, term in months, minus amount)
lblPayment.Text = Pmt(cbxRate.Text.Replace("%", "") / 1200, cbxTerm.Text, -tbxLoan.Text)
'== Display formatted as money: FormatCurrency(blah blah blah)
lblPayment.Text = FormatCurrency(lblPayment.Text, 2)
Else
MessageBox.Show("Error invalid value")
tbxLoan.Focus()
tbxLoan.SelectAll()
End If
End Sub
Upvotes: 0
Reputation: 12196
It's because you're continue to calculate whatever or not the input was valid.
Exit the function by using Return after input was invalid.
Else
MessageBox.Show("Error invalid value")
tbxLoan.Focus()
tbxLoan.SelectAll()
Return
End If
Upvotes: 1