Results 1 to 4 of 4

Thread: Calculating Prices in a Listbox in Visual basic 2010

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    2

    Calculating Prices in a Listbox in Visual basic 2010

    Hi i have recently created a program where meals are selected and added to a list box. I also have a calculate button which when clicked i wish to calculate the prices of the items listed in the list box

    How would I go about doing that. Thanks for your help in advance

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Calculating Prices in a Listbox in Visual basic 2010

    well show what you have started

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,534

    Re: Calculating Prices in a Listbox in Visual basic 2010

    Well you could use a CheckedListBox, set the DataSource to a strong typed container such as a DataTable then for obtaining the total query the underlying DataTable. In Button1 I place the checked items into a new data table that has the same fields as the DataTable which is the source to the CheckedListBox and populate it. After it's populated you have both description and amount for each item. Of course if you were to need quantity that is a slightly different story.

    Have you also considered using a DataGridView?

    Code:
    ''' <summary>
    ''' Requires a CheckedListBox and one button
    ''' </summary>
    ''' <remarks></remarks>
    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 = "Description", .DataType = GetType(String)})
            dt.Columns.Add(New DataColumn With
                           {.ColumnName = "Price", .DataType = GetType(Decimal)})
            dt.Columns.Add(New DataColumn With
                           {.ColumnName = "DisplayText", .DataType = GetType(String),
                            .Expression = "Description + ' - ' + Price"})
    
            dt.Rows.Add(New Object() {"Item1", 23.99D})
            dt.Rows.Add(New Object() {"Second Item", 9.99D})
            dt.Rows.Add(New Object() {"Last Item", 39.99D})
    
            CheckedListBox1.DataSource = dt
            CheckedListBox1.DisplayMember = "DisplayText"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dt As DataTable = CType(CheckedListBox1.DataSource, DataTable).Clone
    
    
            For Each drv As DataRowView In CheckedListBox1.CheckedItems
                dt.Rows.Add(New Object() _
                    {drv.Row.Field(Of String)("Description"), drv.Row.Field(Of Decimal)("Price")})
            Next
    
            Dim Total As Decimal = dt.AsEnumerable.Select(Function(row) row.Field(Of Decimal)("Price")).Sum
    
            MessageBox.Show(Total.ToString)
        End Sub
    End Class
    Name:  15.png
Views: 1470
Size:  14.7 KB

    Here is a version with quantity
    Code:
    ''' <summary>
    ''' Requires a CheckedListBox and one button
    ''' </summary>
    ''' <remarks></remarks>
    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 = "Description", .DataType = GetType(String)})
            dt.Columns.Add(New DataColumn With
                           {.ColumnName = "Price", .DataType = GetType(Decimal)})
            dt.Columns.Add(New DataColumn With
                           {.ColumnName = "Qty", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn With
                           {.ColumnName = "DisplayText", .DataType = GetType(String),
                            .Expression = "Description + ' - ' + Price + ' - ' + Price * Qty"})
    
            dt.Rows.Add(New Object() {"Item1", 23.99D, 2})
            dt.Rows.Add(New Object() {"Second Item", 9.99D, 3})
            dt.Rows.Add(New Object() {"Last Item", 39.99D, 1})
    
            CheckedListBox1.DataSource = dt
            CheckedListBox1.DisplayMember = "DisplayText"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dt As DataTable = CType(CheckedListBox1.DataSource, DataTable).Clone
    
    
            For Each drv As DataRowView In CheckedListBox1.CheckedItems
                dt.Rows.Add(New Object() _
                    {drv.Row.Field(Of String)("Description"),
                     drv.Row.Field(Of Decimal)("Price"),
                     drv.Row.Field(Of Integer)("qty")})
            Next
    
    
            Dim Total As Decimal =
                (
                    From T In dt.AsEnumerable
                    Select T.Field(Of Decimal)("Price") * T.Field(Of Integer)("Qty")
                ).Sum
    
            MessageBox.Show(Total.ToString)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim dt As DataTable = CType(CheckedListBox1.DataSource, DataTable)
            dt.Rows.Add(New Object() {"Another", 1.99D, 2})
        End Sub
    End Class

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    2

    Re: Calculating Prices in a Listbox in Visual basic 2010

    This i what i have done so far

    Code:
    This is what I have done so far
    
    
    Code:
            If radbtnMeal1.Checked = True Then
                lstorder.Items.Add(String.Format(My_Format, "Meal 1", FormatCurrency(PRICE_MEAL1), QtyMeal1.Value, FormatCurrency(QtyMeal1.Value * PRICE_MEAL1)))
    
            End If
    
    
                If RadbtnMeal2.Checked = True Then
                    lstorder.Items.Add(String.Format(My_Format, "Meal 2", FormatCurrency(PRICE_MEAL2), QtyMeal2.Value, FormatCurrency(QtyMeal2.Value * PRICE_MEAL2)))
    
                End If
    
                If RadbtnMeal3.Checked = True Then
                    lstorder.Items.Add(String.Format(My_Format, "Meal 3", FormatCurrency(PRICE_MEAL3), QtyMeal3.Value, FormatCurrency(QtyMeal3.Value * PRICE_MEAL3)))
    
                End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width