Hey guys,
The idea with this form is to add the futures which is calculated when the button is pushed but I need the list to update every time and show the values each time to which the previous value is added to the new calculation, e.g. of what it should look like in listbox:
Year 1: $1,290.93
Year 2: $2,724.32
Year 3: $4,350.76
etc....
I have been working on this a while and I am at a standstill, I was given a function to clear the form everytime it calculates but I don't know how to implement it, I am new to VB. Here is what I have so far:
Public Class frmFutureValue
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
If IsValidData() Then
Dim monthlyInvestment As Decimal _
= Convert.ToDecimal(txtMonthlyInvestment.Text)
Dim yearlyInterestRate As Decimal _
= Convert.ToDecimal(txtInterestRate.Text)
Dim years As Integer = Convert.ToInt32(cmbYears)
Dim monthlyInterestRate As Decimal = yearlyInterestRate / 12 / 100
Dim months As Integer = years * 12
Dim futureValue As Decimal = Me.FutureValue( _
monthlyInvestment, monthlyInterestRate, months)
lstFuture.Text = FormatCurrency(futureValue)
txtMonthlyInvestment.Select()
End If
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf _
& ex.GetType.ToString & vbCrLf & vbCrLf _
& ex.StackTrace, "Exception")
End Try
End Sub
Private Function IsValidData() As Boolean
Return _
IsPresent(txtMonthlyInvestment, "Monthly Investment") AndAlso _
IsDecimal(txtMonthlyInvestment, "Monthly Investment") AndAlso _
IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) AndAlso _
IsPresent(txtInterestRate, "Yearly Interest Rate") AndAlso _
IsDecimal(txtInterestRate, "Yearly Interest Rate") AndAlso _
IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 15)
End Function
Private Function IsPresent(ByVal textBox As TextBox, ByVal name As String) _
As Boolean
If textBox.Text = "" Then
MessageBox.Show(name & " is a required field.", "Entry Error")
textBox.Select()
Return False
End If
Return True
End Function
Private Function IsDecimal(ByVal textBox As TextBox, ByVal name As String) _
As Boolean
Try
Convert.ToDecimal(textBox.Text)
Return True
Catch ex As FormatException
MessageBox.Show(name & " must be a decimal value.", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End Try
End Function
Private Function IsInt32(ByVal textBox As TextBox, ByVal name As String) _
As Boolean
Try
Convert.ToInt32(textBox.Text)
Return True
Catch ex As FormatException
MessageBox.Show(name & " must be an integer value.", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End Try
End Function
Private Function IsWithinRange(ByVal textBox As TextBox, _
ByVal name As String, ByVal min As Decimal, _
ByVal max As Decimal) As Boolean
Dim number As Decimal = CDec(textBox.Text)
If number < min Or number > max Then
MessageBox.Show(name & " must be between " & min & " and " _
& max & ".", "Entry Error")
textBox.Select()
textBox.SelectAll()
Return False
End If
Return True
End Function
Private Function FutureValue(ByVal monthlyInvestment As Decimal, _
ByVal monthlyInterestRate As Decimal, ByVal months As Integer) _
As Decimal
For i As Integer = 1 To months
FutureValue = (FutureValue + monthlyInvestment) _
* (1 + monthlyInterestRate)
Next
End Function
Private Sub ClearFutureValue(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles txtMonthlyInvestment.TextChanged, _
txtInterestRate.TextChanged
lstFuture.Text = ""
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmFutureValue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbYears.Items.Add("1")
cmbYears.Items.Add("2")
cmbYears.Items.Add("3")
cmbYears.Items.Add("4")
cmbYears.Items.Add("5")
cmbYears.Items.Add("6")
cmbYears.Items.Add("7")
cmbYears.Items.Add("8")
cmbYears.Items.Add("9")
cmbYears.Items.Add("10")
cmbYears.Items.Add("11")
cmbYears.Items.Add("12")
cmbYears.Items.Add("13")
cmbYears.Items.Add("14")
cmbYears.Items.Add("15")
cmbYears.Items.Add("16")
cmbYears.Items.Add("17")
cmbYears.Items.Add("18")
cmbYears.Items.Add("19")
cmbYears.Items.Add("20")
cmbYears.SelectedIndex = 2
End Sub
End Class