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

Also when I run it I get this weird error:

"Unable to cast object of type System.Windows.Forms.Combobox to type 'System.IConvertible'.
System.InvalidCastException

at System.Convert.ToInt32(Object value)
at FutureValue.frmFutureValue.btnCalculate_Click(Object sender, EventArgs e) in C/:......."

change

Dim years As Integer = Convert.ToInt32(cmbYears)

to

Dim years As Integer = Convert.ToInt32(cmbYears.selecteditem)

Got the problem now fixed with the number form the combo box, now another problem. I got it to add every time, but I need the output to read as currency as well as show the year. For example:

Year 1: $1,280.93
Year 2: $2,724.32
Year 3: $4,350.76

Here is what I have again:

Imports Microsoft.VisualBasic
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.SelectedItem)

                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()
                If months Mod 12 = 0 Then
                    lstFuture.Items.Add(futureValue)
                End If



            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

The following code will add the value to the end of a string and also convert it for you:

lstFuture.Items.Add(String.Format("Year 1: {0:c}", futureValue))

the '{0:c}' is a place holder, the 0 says to use the first parameter after the string and the :c tells it to format the value as currency. You could also use a counter to keep track of the years:

lstFuture.Items.Add(String.Format("Year {0}: {1:c}", yearCounter, futureValue))

if you increment the counter after adding each line it will give you:

Year 1: $12,000
Year 2: $28,000
etc

Check out this link for a good break down of the options you can use with String.Format

The yearCounter is not declared, am I suppose tp make a variable for that, and thanks for the help, I am grateful!

Yes, the yearCounter was a suggestion. You would have to adjust your calculations though. It look like you just calculate the total for all the years in one go. You would need to use a loop and calculate the amount for each year.
Something like:

dim total as double = 0
for i = 1 to years
   'i have calculated yearTotal seperately incase you want to show each years value seperately
   dim yearTotal as double
   yearTotal = (monthlyInvestment * 12) * (1 + (yearlyInterestRate  / 100))
   total += yearTotal
   lstFuture.Items.Add(String.Format("Year {0}: {1:c}", i, total))
next

Coded on the fly in reply so apologies for any typos, but you get the idea :)

commented: He is super awsome and helpful! +1

It worked! Thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.