循环浏览vb中的列表框并获取总金额

时间:2015-02-27 10:07:13

标签: vb.net loops listbox

我正在尝试遍历列表框中的价格列表,并在表单上的文本框中打印总计。循环我有作品,但它产生的数字都是错的!谁能帮助我?

数字看起来还不错,但是当我在列表框中添加更多项目时,总金额出错了。

Public Class f

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'LoftDataSet.Services' table. You can move, or remove it, as needed.
    Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)

    'Loop through all the rows that are in the dataset
    For Each dr As DataRow In LoftDataSet.Services.Rows

        Dim btn As New Button 'Instantiate a button
        btn.Text = dr("service_name").ToString 'UserName is a field in my Users Table
        btn.Size = New Size(140, 80)
        btn.Tag = dr("ID") 'Here we set the tag to the primary key (ID)

        'Since we're using a flowlayoutpanel, we don't need to worry about setting the location property
        FlowLayoutPanel1.Controls.Add(btn) 'Add the button to the flow layout panel
        AddHandler btn.Click, AddressOf UserClick  'Here we give the button a handler for the click event

    Next
End Sub

Public Class Product
    Public Property ProductName As String
    Public Property ProductCost As Decimal

    Public Overrides Function ToString() As String
        Return ProductCost & "      " & ProductName
        'Here you could build a formatted string to the user to include cost
    End Function
End Class

'Here we write our method for the click event of the button(s) we created
Dim SelectedProduct As New Product
Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs)

    'We set a filter to the binding source passing it ID=<and whatever is stored in the tag property>

    ServicesBindingSource.Filter = "ID = " & DirectCast(sender, Button).Tag.ToString

    SelectedProduct.ProductName = DirectCast(sender, Button).Text
    SelectedProduct.ProductCost = DirectCast(ServicesBindingSource(0), DataRowView).DataView(0)(2)
    ListBox1.Items.Add(SelectedProduct)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))
    Next
End Sub




Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim aValue As Decimal
    For Each item As Product In ListBox1.Items
        aValue += CDec(item.ProductCost)
    Next
    TextBox1.Text = aValue



End Sub

结束班

1 个答案:

答案 0 :(得分:0)

使用整数作为计数循环遍历每个项目。

For i As Integer = 0 To ListBox1.Items.Count - 1
        MsgBox(ListBox1.Items(i).ToString)    
Next

或者您可以将其添加到整数/双精度值

For i As Integer = 0 To ListBox1.Items.Count - 1
        MyInteger += Double.Parse.ListBox1.Items(i).ToString) 
Next
MsgBox(MyInteger)
'Now the messagebox will show your total

编辑: 不要使用ListBo1.Items.Item(i)。直接通过Listbox1.Items(i)

访问