Assistance with MsgBox in Program
OK, it's the newbie to programming. I have written a program to calcuate and display the montly payment and total interest paid on a loan. I have to put in a note that says if interest rate is below .06 that "Interest Rate Must Be 6% or higher." I don't have a problem with the message but after the message is displayed and I hit ok, it still calculates the amount. How do I get it to either, return to program to input new interest rate then complete program: Code is as follows:
Code:
Public Class frmLoan
Private Sub btnAnalyze_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
Dim p, r, n, payment, ti As Double
' Set MSGbox if percentage is off
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
Else
End If
'set value parameters for calculation
p = CDbl(txtloan.Text)
r = CDbl(txtRate.Text) / 12
n = CDbl(txtLength.Text) ' duration of loan
payment = (p * r) / (1 - (1 + r) ^ (-n))
ti = (n * payment) - p
'display results from formula calculations
With lstResults.Items
.Clear()
.Add("Total monthly pay is " & (FormatCurrency(payment) & "."))
.Add("")
.Add("Total interest is paid is " & (FormatCurrency(ti) & "."))
End With
End Sub
End Class
Re: Assistance with MsgBox in Program
All the other code (calculations) should go under the else in the if statement. So if the amount is > 0.06 then it will calculate.
Re: Assistance with MsgBox in Program
Welcome to the forum!
If you would use the code-tags, we could read your code more easily.
You checked for r<.06 and if that's true you show the messagebox, I figure you don't want to calculate with a interest <.06, if that's true put EXIT SUB after the messagebox-line. That way the calculation will not be done. I would also suggest to put the focus on the control where the interest is set (that should be text1.text) and maybe put in a correct value as well.
Re: Assistance with MsgBox in Program
If there is no Else condition, then you don't need it.
Code:
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
End If
However, in this case, I believe there will be an Else condition as stated by Hell-Lord. That would be where your alternate calculation would go.
Re: Assistance with MsgBox in Program
It should be either:
Code:
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
Exit Sub
End If
....
....
or
Code:
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
Else
......
......
End If
Where " .... ...." denotes all the rest of the calculations!
Re: Assistance with MsgBox in Program
Quote:
Originally Posted by opus
It should be either:
Code:
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
Exit Sub
End If
....
....
or
Code:
If r < 0.06 Then
MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
Else
......
......
End If
Where " .... ...." denotes all the rest of the calculations!
The second option should always be preferred.
Re: Assistance with MsgBox in Program
If you say so.
BTW Knut doesn't look that cute anymore!
Re: Assistance with MsgBox in Program
Using an Exit Sub is creating spaghetti code, although it's not an extreme example thereof. The second option is more efficient and easier to read.
Re: Assistance with MsgBox in Program