5 Replies - 573 Views - Last Post: 01 October 2012 - 06:42 AM Rate Topic: -----

#1 compucrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Vb mortgage program won't display totals...

Posted 30 September 2012 - 02:00 PM

 Please help, I think I did all the calculations correctly, but when I hit the analyze button nothing happens when it is supposed to perform the calculations and output the results in the two textboxes. Please help!

Attached is what it is supposed to do...mine looks right but just doesn't output the calculations.

Thank you!!!!


Dim TotalInterest As Double
Dim Payment As Double
Dim P As Double
Dim R As Double
Dim N As Integer

Private Sub btnAnalyze_Click(sender As System.Object, e As System.EventArgs) Handles btnAnalyze.Click

P = txtAmount.Text
N = txtDuration.Text
R = txtInterestRate.Text

Payment = P * R / (1 - (1 + R) ^ (-N))
TotalInterest = N * Payment - P


Double.TryParse(txtPayment.Text, Payment)
Double.TryParse(txtInterest.Text, TotalInterest)

If Not Double.TryParse(txtAmount.Text, P) Then
MessageBox.Show("Please enter in loan amount")
End If

If Not Integer.TryParse(txtDuration.Text, N) Then
MessageBox.Show("Please enter in duration of loan")
End If

If Not Double.TryParse(txtInterestRate.Text, R) Then
MessageBox.Show("Please enter in interest rate")
End If



Here is the attachment...

Is This A Good Question/Topic? 0
  • +

Replies To: Vb mortgage program won't display totals...

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: Vb mortgage program won't display totals...

Posted 30 September 2012 - 02:32 PM

For starters, this is VB.NET, not VB6.

Let's step through your code:

Dim TotalInterest As Double
Dim Payment As Double
Dim P As Double
Dim R As Double
Dim N As Integer


You should really use more descriptive variable names. P, R and N tell me nothing about their purpose.

Private Sub btnAnalyze_Click(sender As System.Object, e As System.EventArgs) Handles btnAnalyze.Click

P = txtAmount.Text
N = txtDuration.Text
R = txtInterestRate.Text


This isn't right. You should be using Double.TryParse and Integer.TryParse to get the Double or Integer values from a string.

Payment = P * R / (1 - (1 + R) ^ (-N))
TotalInterest = N * Payment - P


I'm going to just assume these are correct.

Double.TryParse(txtPayment.Text, Payment)
Double.TryParse(txtInterest.Text, TotalInterest)


I'm assuming you're trying to set the value of the two text fields -- you're doing it wrong. Double.TryParse attempts to get a double from the value in a string -- meaning it's trying to get the value of txtPayment.Text and txtInterest.Text, not setting anything to that. You'll want to go with txtPayment.Text = Payment.

If Not Double.TryParse(txtAmount.Text, P) Then
MessageBox.Show("Please enter in loan amount")
End If

If Not Integer.TryParse(txtDuration.Text, N) Then
MessageBox.Show("Please enter in duration of loan")
End If

If Not Double.TryParse(txtInterestRate.Text, R) Then
MessageBox.Show("Please enter in interest rate")
End If


This should be up at the start, in place of the code you're trying to use to set the values of the integers. Not down here. And simply showing a message box up there won't do -- you'll want to stop the function at that point, as well (otherwise you're making pointless calculations on erroneous data).
Was This Post Helpful? 0
  • +
  • -

#3 compucrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Vb mortgage program won't display totals...

Posted 30 September 2012 - 06:44 PM

Ok I made those corrections and now I just get zero's in the textboxes (progress from before) but still not doing the calculations? What else am I doing wrong?


 Dim TotalInterest As Double
    Dim Payment As Double
    Dim P As Double
    Dim R As Double
    Dim N As Integer

    Private Sub btnAnalyze_Click(sender As System.Object, e As System.EventArgs) Handles btnAnalyze.Click

        Double.TryParse(txtAmount.Text, P)
        Double.TryParse(txtDuration.Text, N)
        Double.TryParse(txtInterestRate.Text, R)
        Double.TryParse(txtPayment.Text, Payment)
        Double.TryParse(txtInterest.Text, TotalInterest)

        If Not Double.TryParse(txtAmount.Text, P) Then
            MessageBox.Show("Please enter in loan amount")
        End If

        If Not Integer.TryParse(txtDuration.Text, N) Then
            MessageBox.Show("Please enter in duration of loan")
        End If

        If Not Double.TryParse(txtInterestRate.Text, R) Then
            MessageBox.Show("Please enter in interest rate")
        End If


        txtPayment.Text = Payment
        txtInterest.Text = TotalInterest

        Payment = (P * R / (1 - (1 + R) ^ (-N)))
        TotalInterest = (N * Payment - P)

      
    End Sub



Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • Joined: 26-March 09

Re: Vb mortgage program won't display totals...

Posted 30 September 2012 - 11:50 PM

You're updating your textboxes before doing the calculations......

       txtPayment.Text = Payment
       txtInterest.Text = TotalInterest

       Payment = (P * R / (1 - (1 + R) ^ (-N)))
       TotalInterest = (N * Payment - P)

Was This Post Helpful? 1
  • +
  • -

#5 compucrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Vb mortgage program won't display totals...

Posted 01 October 2012 - 05:25 AM

what does that mean exactly?
Was This Post Helpful? 0
  • +
  • -

#6 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Vb mortgage program won't display totals...

Posted 01 October 2012 - 06:42 AM

View Postcompucrazy, on 01 October 2012 - 08:25 AM, said:

what does that mean exactly?

Exactly what it says.

do the math first.

Payment = (P * R / (1 - (1 + R) ^ (-N)))
TotalInterest = (N * Payment - P)

txtPayment.Text = Payment
txtInterest.Text = TotalInterest



also, you need to convert the numbers back into strings before you can apply them to a label.
you can use

txtPayment.Text = Payment.tostring("C2")
txtInterest.Text = TotalInterest.tostring("C2")


gives them currency format with two decimal places
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1