|
-
Jul 17th, 2007, 02:37 AM
#1
Thread Starter
New Member
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
Last edited by Hack; Jul 17th, 2007 at 06:50 AM.
Reason: Added [code][/code] tags
-
Jul 17th, 2007, 02:40 AM
#2
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.
-
Jul 17th, 2007, 02:43 AM
#3
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.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jul 17th, 2007, 06:52 AM
#4
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.
-
Jul 17th, 2007, 06:58 AM
#5
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!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jul 17th, 2007, 08:13 AM
#6
Re: Assistance with MsgBox in Program
 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.
-
Jul 17th, 2007, 08:20 AM
#7
Re: Assistance with MsgBox in Program
If you say so.
BTW Knut doesn't look that cute anymore!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jul 17th, 2007, 08:31 AM
#8
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.
-
Jul 17th, 2007, 12:21 PM
#9
Re: Assistance with MsgBox in Program
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|