-1

Basically I have a checked list box and each of the items in it have a corresponding price. How do i code it so that

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

2) how do I then add up those prices (for example if several items have been checked, I want to add up the prices and display the total in a text box).

I'm doing this on "Visual Studio Express for desktop" so basically vb.net

Click here to see the picture of the CheckedListBox

Víctor Gómez
  • 724
  • 6
  • 23
  • 1
    CheckedListBox is not a collection class, it is merely good enough to show the content of a collection. Separate the data from the view, declare a List(Of Something) variable. Where Something is a class that at least has Selected As Boolean and Price As Decimal properties and a ToString() override. Now it is simple. – Hans Passant Dec 22 '15 at 12:37

1 Answers1

0

Here is a conceptual example where I am using Console.WriteLine in a Window form project where the output is shown in Visual Studio's output window. Taken from the following article I wrote a while back and adapted to assist here.

One CheckedListBox, one 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
Karen Payne
  • 4,341
  • 2
  • 14
  • 31