对于Next Loop计算运行总计不会起作用

时间:2014-07-18 20:25:28

标签: list for-loop listbox next running-total

我的大多数代码都在“计算总计”按钮之外工作。我有三个列表框,需要我的'Calculate Totals'按钮来迭代'lstCosts'列表框中的选定成本。但是在Option Strict On中我收到一个错误,因为“List不是Double的成员”,而“ListCount不是Double的成员”有人可以帮忙纠正这个问题吗?我知道我的代码已接近工作我只是不知道如何。

以下是我的代码无效:

Private Sub btnAddCourse_Click(sender As System.Object, e As System.EventArgs) Handles btnAddCourse.Click

    'Declare variables
    Dim strCourse As String 'To hold the Course Values
    Dim strLocation As String 'To hold the Location values

    'Item Indexing
    'Identifies the four Course strings
    strCourse = lstCourse.Items(0).ToString()
    strCourse = lstCourse.Items(1).ToString()
    strCourse = lstCourse.Items(2).ToString()
    strCourse = lstCourse.Items(3).ToString()

    'Identifies the four Location strings
    strLocation = lstLocation.Items(0).ToString()
    strLocation = lstLocation.Items(1).ToString()
    strLocation = lstLocation.Items(2).ToString()
    strLocation = lstLocation.Items(3).ToString()


    If lstCourse.SelectedIndex = -1 Then
        'Error Message for no course selected
        MessageBox.Show("Select a course.", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
    ElseIf lstLocation.SelectedIndex = -1 Then
        'Error message for no location selected
        MessageBox.Show("Select a location.", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        'Essential Linux and Belfast selected = £705
    ElseIf lstCourse.SelectedIndex = 0 And lstLocation.SelectedIndex = 0 Then
        lstCosts.Items.Add(705)
        'Essential Linux and Coleraine selected = £600
    ElseIf lstCourse.SelectedIndex = 0 And lstLocation.SelectedIndex = 1 Then
        lstCosts.Items.Add(600)
        'Essential Linux and Jordasntown selected = £600
    ElseIf lstCourse.SelectedIndex = 0 And lstLocation.SelectedIndex = 2 Then
        lstCosts.Items.Add(600)
        'Essential Linux and Magee selected = £630
    ElseIf lstCourse.SelectedIndex = 0 And lstLocation.SelectedIndex = 3 Then
        lstCosts.Items.Add(630)
        'Project Management and Belfast selected £520
    ElseIf lstCourse.SelectedIndex = 1 And lstLocation.SelectedIndex = 0 Then
        lstCosts.Items.Add(520)
        'Project Management and Coleraine selected = £450
    ElseIf lstCourse.SelectedIndex = 1 And lstLocation.SelectedIndex = 1 Then
        lstCosts.Items.Add(450)
        'Project Management and Jordanstown selected = £450
    ElseIf lstCourse.SelectedIndex = 1 And lstLocation.SelectedIndex = 2 Then
        lstCosts.Items.Add(450)
        'Project Management and Magee selected = £470
    ElseIf lstCourse.SelectedIndex = 1 And lstLocation.SelectedIndex = 3 Then
        lstCosts.Items.Add(470)
        'Overview of net and Belfast selected = £705
    ElseIf lstCourse.SelectedIndex = 2 And lstLocation.SelectedIndex = 0 Then
        lstCosts.Items.Add(705)
        'Overview of net and Coleraine selected = £575
    ElseIf lstCourse.SelectedIndex = 2 And lstLocation.SelectedIndex = 1 Then
        lstCosts.Items.Add(575)
        'Overview of net and Jordanstown selected = £575
    ElseIf lstCourse.SelectedIndex = 2 And lstLocation.SelectedIndex = 2 Then
        lstCosts.Items.Add(575)
        'Overview of net and Magee selected = £605
    ElseIf lstCourse.SelectedIndex = 2 And lstLocation.SelectedIndex = 3 Then
        lstCosts.Items.Add(605)
        'PHP and Belfast selected = £780
    ElseIf lstCourse.SelectedIndex = 3 And lstLocation.SelectedIndex = 0 Then
        lstCosts.Items.Add(780)
        'PHP and Coleraine selected = £675
    ElseIf lstCourse.SelectedIndex = 3 And lstLocation.SelectedIndex = 1 Then
        lstCosts.Items.Add(675)
        'PHP and Jordanstown selected = £675
    ElseIf lstCourse.SelectedIndex = 3 And lstLocation.SelectedIndex = 2 Then
        lstCosts.Items.Add(675)
        'PHP and Magee selected = £705
    ElseIf lstCourse.SelectedIndex = 3 And lstLocation.SelectedIndex = 3 Then
        lstCosts.Items.Add(705)
    End If

End Sub

Private Sub btnCalculateTotal_Click(sender As Object, e As EventArgs) Handles btnCalculateTotal.Click

    Dim lstCosts As Double
    Dim lblTotalCost As Double

    For lstCosts = 0 To lstCosts.ListCount - 1
        lblTotalCost = lblTotalCost + CDbl(lstCosts.List(lstCosts))
    Next lstCosts

    lblTotalCost = lstCosts

End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click

    'Clears the fields
    lstCourse.ClearSelected()
    lstLocation.ClearSelected()
    lstCosts.Items.Clear()
    lblTotalCost.Text = String.Empty

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

    'Closes the Program
    Me.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

当我看到之前有更多代码来支持我的解决方案。 我认为你有2个本地与全局命名和分配问题。

Private Sub btnCalculateTotal_Click(sender As Object, e As EventArgs) Handles btnCalculateTotal.Click

'Declare variables
Dim lvIterate As Double '
Dim lvTotalOfCost As Double

'Calculate Total Cost
For lvIterate = 0 To lstCosts.ListCount - 1

    lvTotalOfCost = lvTotalOfCost + CDbl(lstCosts.List(lvIterate))
Next

lblTotalCost.Text = lvTotalOfCost

End Sub

希望它能解决你的问题。