0

好的,所以我找到的所有答案都与子程序有关。我不需要我需要的是根据组合框选择从数组中填充列表框。它必须由 btnCalculate 块激活

例如,如果组合框选择为 3,我需要列表框来填充 futureValueArray(0) futureValueArray(1) futureValueArray(2)

这是我的 btnCalculate 程序代码

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(cboYears.Text)

            Dim monthlyInterestRate As Decimal = yearlyInterestRate / 12 / 100
            Dim months As Integer = years * 12

            Dim futureValue As Decimal = Me.FutureValue(
                monthlyInvestment, monthlyInterestRate, months)

            Dim futureValueArray(19) As String
            futureValueArray(0) = "Year 1: " & CStr((monthlyInvestment * 12) * yearlyInterestRate)
            futureValueArray(1) = "Year 2: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 2)
            futureValueArray(2) = "Year 3: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 3)
            futureValueArray(3) = "Year 4: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 4)
            futureValueArray(4) = "Year 5: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 5)
            futureValueArray(5) = "Year 6: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 6)
            futureValueArray(6) = "Year 7: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 7)
            futureValueArray(7) = "Year 8: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 8)
            futureValueArray(8) = "Year 9: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 9)
            futureValueArray(9) = "Year 10: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 10)
            futureValueArray(10) = "Year 11: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 11)
            futureValueArray(11) = "Year 12: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 12)
            futureValueArray(12) = "Year 13: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 13)
            futureValueArray(13) = "Year 14: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 14)
            futureValueArray(14) = "Year 15: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 15)
            futureValueArray(15) = "Year 16: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 16)
            futureValueArray(16) = "Year 17: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 17)
            futureValueArray(17) = "Year 18: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 18)
            futureValueArray(18) = "Year 19: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 19)
            futureValueArray(19) = "Year 20: " & CStr(((monthlyInvestment * 12) * yearlyInterestRate) * 20)

            ListBoxFutureValue.Text = FormatCurrency(futureValue)
            txtMonthlyInvestment.Select()

            ListBoxFutureValue.Items.Add(futureValue)
        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 Sub frmFutureValue_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim items() As String =
    {"Please select a number: ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}
    For Each item As String In items
        cboYears.Items.Add(item)
    Next

    If cboYears.Items.Count > 0 Then
        cboYears.SelectedIndex = 3    ' The first item has index 0 '
    End If

End Sub

根据我的想法,我需要一个 for while 循环,但我不知道如何比较两者以填充列表框。我很迷茫。大脑没有工作

4

1 回答 1

1

不确定我是否正确理解了这个问题,但如果我理解了,你想要的代码是

    Dim selectedNumber As Integer
    Integer.TryParse(cboYears.SelectedItem, selectedNumber)
    ListBoxFutureValue.Items.Clear()
    For i = 0 To selectedNumber - 1
        ListBoxFutureValue.Items.Add(futureValueArray(i))
    Next
于 2013-04-10T05:01:06.057 回答