对于下一个循环来计算运行总计将不起作用

Posted

技术标签:

【中文标题】对于下一个循环来计算运行总计将不起作用【英文标题】:For Next Loop to calculate running total won't work 【发布时间】:2014-07-18 20:25:27 【问题描述】:

我的大部分代码都可以在“计算总计”按钮之外使用。我有三个列表框,需要我的“计算总计”按钮来遍历“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】:

当我查看之前有更多的代码来支持我的解决方案。 我认为您有 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

结束子

希望它能解决你的问题。

【讨论】:

这似乎仍然不起作用,它仍然存在完全相同的问题,'ListCount' 和 '.list' 不是 Double 的成员 lstCosts 是什么类型的控制? vb中的listBox和listView使用“lstCosts.Items.Count” "lstCosts.Items.Count" 似乎也不起作用我很困惑。 lstCosts 是包含成本的列表框的名称。遍历列表框中的每个成本后,总成本标签“lblTotalCost”应显示所有这些成本的总和 我模拟了页面,添加了潜艇。我做了一些挖掘,这就是我得到的 Private Sub btnCalculateTotal_Click(sender As Object, e As EventArgs) Handles btnCalculateTotal.Click Dim lvTotalCost As Double Dim x As Integer For Each lvitem As ListViewItem In Me.lstCosts.Items x = CDbl(lvitem .Text) lvTotalCost += x Next lblTotalCost.Text = lvTotalCost End Sub 看看它在你的应用程序中是如何工作的记得评论原文我会坚持几天的项目 Private Sub btnCalculateTotal_Click(sender As Object, e As EventArgs) Handles btnCalculateTotal.Click Dim lvTotalCost As Double Dim x As Integer For Each lvitem As ListViewItem In Me.lstCosts.Items x = CInt(CDbl(lvitem .Text)) lvTotalCost += x Next lvitem = lvTotalCost End Sub

以上是关于对于下一个循环来计算运行总计将不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用节点运行脚本的计划任务将不起作用

QGraphicsSceneMouseEvent 获取位置将不起作用[重复]

express.js,如果我删除下一个参数,错误处理程序将不起作用

在状态下设置从 API 获取的数据将不起作用

SharePoint API GetFolderByServerRelativeUrl 将不起作用 GetFileByServerRelativeUrl 将起作用

启用主题的 Windows XP/Vista 中的 TAnimate 将不起作用 [关闭]