aclaus225
asked on
Listbox Value Not Registering for Calculation
I am trying to build a mortgage calculator application with VB and I have list box to select the number of years, but it does not seem to be actually factoring into the solution.
This is the code:
e) should tell me the value that is selected, but that part of the equation is just coming up zero. Any ideas on what I did wrong?
This is the code:
Private Sub calcbutton_Click(sender As System.Object, e As System.EventArgs) Handles calcbutton.Click
'The car price should appear in terms of dollars and cents.
Dim carprice As Double
'The APR needs two percentage points
Dim interestrate As Double
'Money down should be dollars and cents
Dim moneydown As Double
'Dim loan term As Double
Dim loanterm As Double
'Dim Payment as Double
Dim payment As Double
Dim monthlyinterest As Double
Dim months As Double
Dim loanamount As Double
'Declare where the values are coming from
carprice = Val(Carpricetext.Text)
interestrate = Val(IRTextbox.Text)
moneydown = Val(Dwnpymnttext.Text)
loanterm = Val(LnTrmList.SelectedValue)
loanamount = (carprice - moneydown)
Do While loanterm <= 5
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
'calculate payments - rate * month = monthly payments
payment = _
Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = payment
Loop
End Sub
loanterm = Val(LnTrmList.SelectedValuASKER
MsgBox after the calculation showed that Months was registering as 0.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I have:
And it still does not seem to do the calculation.
It will not do the payment = line as "Argument 'NPer' is not a valid value."
'The car price should appear in terms of dollars and cents.
Dim carprice As Double
'The APR needs two percentage points
Dim interestrate As Double
'Money down should be dollars and cents
Dim moneydown As Double
'Dim loan term As Double
Dim loanterm As Double
'Dim Payment as Double
Dim payment As Double
Dim monthlyinterest As Double
Dim months As Double
Dim loanamount As Double
'Declare where the values are coming from
carprice = Val(Carpricetext.Text)
interestrate = Val(IRTextbox.Text)
moneydown = Val(Dwnpymnttext.Text)
loanterm = Val(LnTrmList.SelectedItem)
loanamount = (carprice - moneydown)
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
'calculate payments - rate * month = monthly payments
payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = payment
End Sub
And it still does not seem to do the calculation.
It will not do the payment = line as "Argument 'NPer' is not a valid value."
What kind of values do you have in your ListBox? I put in years, such as: 3, 4, 5 ,6 , 7, etc...
ASKER
The values in the listbox are:
1
2
3
4
5
1
2
3
4
5
NPer is the number of months in the PMT calculation.
If your number of months is still showing as "0" then your calculation won't work.
I get the error "Argument 'NPer' is not a valid value." if I do this calculation:
Payment = Pmt(0.25, 0, 2000)
are you using SelectedItem or SelectedItem.value? Does it make a difference?
If your number of months is still showing as "0" then your calculation won't work.
I get the error "Argument 'NPer' is not a valid value." if I do this calculation:
Payment = Pmt(0.25, 0, 2000)
are you using SelectedItem or SelectedItem.value? Does it make a difference?
After the changes, it worked perfectly for me. =\
You could also make sure there is an item selected in the ListBox:
If LnTrmList.SelectedIndex <> -1 Then
loanterm = Val(LnTrmList.SelectedItem )
loanamount = (carprice - moneydown)
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
Debug.Print("loanterm = " & loanterm)
Debug.Print("months = " & months)
'calculate payments - rate * month = monthly payments
payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = payment
Else
MessageBox.Show("Please select a Term!")
End If
With the Debug.Prints() in there, what do you get in the Immediate window?
You could also make sure there is an item selected in the ListBox:
If LnTrmList.SelectedIndex <> -1 Then
loanterm = Val(LnTrmList.SelectedItem
loanamount = (carprice - moneydown)
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
Debug.Print("loanterm = " & loanterm)
Debug.Print("months = " & months)
'calculate payments - rate * month = monthly payments
payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = payment
Else
MessageBox.Show("Please select a Term!")
End If
With the Debug.Prints() in there, what do you get in the Immediate window?
ASKER
Predictably, I get: Please Select a Term. I have no other code on this form.
This is all the code on this page:
Option Strict On
Public Class childform
Private Sub calcbutton_Click(sender As System.Object, e As System.EventArgs) Handles calcbutton.Click
'The car price should appear in terms of dollars and cents.
Dim carprice As Double
'The APR needs two percentage points
Dim interestrate As Double
'Money down should be dollars and cents
Dim moneydown As Double
'Dim loan term As Double
Dim loanterm As Double
'Dim Payment as Double
Dim payment As Double
Dim monthlyinterest As Double
Dim months As Double
Dim loanamount As Double
'Declare where the values are coming from
carprice = Val(Carpricetext.Text)
interestrate = Val(IRTextbox.Text)
moneydown = Val(Dwnpymnttext.Text)
loanterm = CDbl(LnTrmList.SelectedIte m)
If LnTrmList.SelectedIndex <> -1 Then
loanterm = Val(LnTrmList.SelectedItem )
loanamount = (carprice - moneydown)
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
Debug.Print("loanterm = " & loanterm)
Debug.Print("months = " & months)
'calculate payments - rate * month = monthly payments
payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = CStr(payment)
Else
MessageBox.Show("Please select a Term!")
End If
End Sub
End Class
This is all the code on this page:
Option Strict On
Public Class childform
Private Sub calcbutton_Click(sender As System.Object, e As System.EventArgs) Handles calcbutton.Click
'The car price should appear in terms of dollars and cents.
Dim carprice As Double
'The APR needs two percentage points
Dim interestrate As Double
'Money down should be dollars and cents
Dim moneydown As Double
'Dim loan term As Double
Dim loanterm As Double
'Dim Payment as Double
Dim payment As Double
Dim monthlyinterest As Double
Dim months As Double
Dim loanamount As Double
'Declare where the values are coming from
carprice = Val(Carpricetext.Text)
interestrate = Val(IRTextbox.Text)
moneydown = Val(Dwnpymnttext.Text)
loanterm = CDbl(LnTrmList.SelectedIte
If LnTrmList.SelectedIndex <> -1 Then
loanterm = Val(LnTrmList.SelectedItem
loanamount = (carprice - moneydown)
monthlyinterest = (interestrate / 100) / 12
months = 12 * loanterm
Debug.Print("loanterm = " & loanterm)
Debug.Print("months = " & months)
'calculate payments - rate * month = monthly payments
payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
paymenttxt.Text = CStr(payment)
Else
MessageBox.Show("Please select a Term!")
End If
End Sub
End Class
I'm now in front of a VB machine as well.
I've recreated the problem quite simply (not bothered naming my textboxes etc).
And this works!
I've recreated the problem quite simply (not bothered naming my textboxes etc).
And this works!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Payment As Double
Dim loanamount As Double = (Val(TextBox1.Text) - Val(TextBox3.Text))
Dim monthlyinterest As Double = (Val(TextBox2.Text) / 100) / 12
Dim loanterm = Val(ListBox1.SelectedItem)
Dim months = 12 * loanterm
'calculate payments - rate * month = monthly payments
Payment = Pmt(monthlyinterest, months, -loanamount)
'displays results
TextBox4.Text = Payment
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("1")
ListBox1.Items.Add("2")
ListBox1.Items.Add("3")
ListBox1.Items.Add("4")
ListBox1.Items.Add("5")
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CSIP,
The only difference between your form and mine is that the listbox is shrunken to only show one value at a time with an arrow.
The only difference between your form and mine is that the listbox is shrunken to only show one value at a time with an arrow.
ASKER
Wow, I hate to admit it, but my mistake with this was simply that I did not select the number (highlight).
Is your list box set to allow multiple selections?
If so, you need to read the selected values in a different way.
I'll set my box like that and see if it makes a difference.
If so, you need to read the selected values in a different way.
I'll set my box like that and see if it makes a difference.
Doh!
Glad you got to the bottom of it!
Glad you got to the bottom of it!
Fair split. I'm happy :-)
Hehe!...did you by chance stay up all night?
Drink some coffee bud. ;)
Drink some coffee bud. ;)
I'm not in front of a box with VB on it, but I think that's where you're slightly awry.
Use your trace environment and watches to keep an eye on LnTrnList.SelectedItem.Val