-1

基本上我有一个选中的列表框,其中的每个项目都有相应的价格。我该如何编码

1) when one of the items is selected, the relating price is stored somewhere in the code, and

2)然后我如何将这些价格相加(例如,如果已经检查了几个项目,我想将价格相加并在文本框中显示总计)。

我在“Visual Studio Express for desktop”上这样做,所以基本上是 vb.net

点击这里查看CheckedListBox的图片

4

1 回答 1

0

这是一个概念性示例,我在 Window 窗体项目中使用 Console.WriteLine,其中输出显示在 Visual Studio 的输出窗口中。摘自我不久前写的以下文章,并在这里提供了帮助。

一个 CheckedListBox,一个 Button

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn With {.ColumnName = "Identifier",
                                                    .DataType = GetType(Int32),
                                                    .AutoIncrement = True,
                                                    .AutoIncrementStep = 100,
                                                    .AutoIncrementSeed = 100})

        dt.Columns.Add(New DataColumn With {.ColumnName = "ItemName",
                                            .DataType = GetType(String)})

        dt.Columns.Add(New DataColumn With {.ColumnName = "Cost",
                                            .DataType = GetType(Decimal)})

        dt.Columns.Add(New DataColumn With {.ColumnName = "ExtraData",
                                            .DataType = GetType(String)})

        dt.Rows.Add(New Object() {Nothing, "One", 10.99D, "Extra 1"})
        dt.Rows.Add(New Object() {Nothing, "Two", 7D, "Extra 2"})
        dt.Rows.Add(New Object() {Nothing, "Three", 5.99D, "Extra 3"})
        dt.Rows.Add(New Object() {Nothing, "Four", 1.34D, "Extra 4"})
        dt.Rows.Add(New Object() {Nothing, "Five", 45.65D, "Extra 5"})

        clbCheckedListBox.DataSource = dt
        clbCheckedListBox.DisplayMember = "ItemName"

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If clbCheckedListBox.CheckedItems.Count > 0 Then
            For Each drv As DataRowView In clbCheckedListBox.CheckedItems
                Console.WriteLine("{0} - {1}", drv.Row.Field(Of String)("ItemName"), drv.Row.Field(Of Decimal)("Cost"))
            Next
            Dim total As Decimal = clbCheckedListBox.CheckedItems.Cast(Of DataRowView).Select(Function(view) view.Row.Field(Of Decimal)("Cost")).Sum
            Console.WriteLine(total)
        End If
    End Sub
End Class
于 2015-12-22T14:51:15.450 回答