如何将一个列表框中的项的总和相乘?

伊森

因此,我正在编写一个程序,您可以在其中从组合框中选择产品,选择数量,然后它将在列表框中返回价格和数量。

但是,我希望能够将每行产品的价格乘以数量,然后将所有行的成本相加得出“总成本”。我该怎么做?

形式如下:

表格

TL; DR如何将每条产品的价格相乘,然后将每条产品的总价格相加。谢谢你!!

这是我的代码:IGNORE OLEDB部件-有些命名不正确,因为它是WIP

Imports System.Data.OleDb
Public Class frmOrderProduct
    Dim iPadCasePrice As String
    Dim iPad2Price As String
    Dim prod_list As List(Of String)

    Private Sub frmOrderProduct_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        cmboxProduct.Items.Add("iPad Case")
        cmboxProduct.Items.Add("iPad 2")

        iPadCasePrice = "$20.00"
        iPad2Price = "$200.00"


        txtUsername.Text = frmLogin.txtUsername.Text

    End Sub

    Private Sub btnAddToCart_Click(sender As Object, e As EventArgs) Handles btnAddToCart.Click

        If cmboxProduct.SelectedItem = "" Or txtQuantity.Text = "" Then
            MsgBox("Please fill out all required fields", MsgBoxStyle.OkOnly, "Invalid entered data")
        Else
            ListBoxCart.Items.Add("x" & txtQuantity.Text & " " & cmboxProduct.SelectedItem & " " & txtPriceEach.Text)

            'txtShoppingCart.AppendText("x" & txtQuantity.Text & " " & Product1 & Environment.NewLine)
        End If
    End Sub

    Private Sub txtQuantity_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtQuantity.KeyPress

        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If

    End Sub

    Private Sub btnOrderProducts_Click(sender As Object, e As EventArgs) Handles btnOrderProducts.Click

        Dim numberOfItems = ListBoxCart.Items.Count
        For Each item As String In ListBoxCart.Items

            Try

                cm = New OleDbCommand
                With cm
                    .Connection = cn
                    .CommandType = CommandType.Text
                    .CommandText = "INSERT INTO tblOrders ([ProductName],[Quantity],[PriceEach],[TotalPrice],[Username]) VALUES (@ProductName,@Quantity,@PriceEach,@TotalPrice,@Username)"

                    prod_list = item.Split(" ").ToList
                    Dim prod_name = prod_list.ElementAt(1)
                    Dim prod_quantity = Integer.Parse(prod_list.ElementAt(0).Remove("x"))
                    Dim price_each = prod_list.ElementAt(2)

                    .Parameters.Add(New OleDbParameter("@ProductName", OleDbType.VarChar, 255, prod_name))
                    .Parameters.Add(New OleDbParameter("@Quantity", OleDbType.VarChar, 255, prod_quantity))
                    .Parameters.Add(New OleDbParameter("@PriceEach", OleDbType.VarChar, 255, price_each))
                    .Parameters.Add(New OleDbParameter("@TotalPrice", OleDbType.VarChar, 255, price_each))
                    .Parameters.Add(New OleDbParameter("@Username", OleDbType.VarChar, 255, txtUsername.Text))
                    prod_list = New List(Of String)

                    cm.Parameters("@ProductName").Value = prod_name
                    cm.Parameters("@Quanity").Value = prod_quantity
                    cm.Parameters("@PriceEach").Value = price_each
                    cm.Parameters("@TotalPrice").Value =
                    cm.Parameters("@Username").Value = txtUsername.Text

                    cm.ExecuteNonQuery()
                    MsgBox("Record saved.", MsgBoxStyle.Information)
                    cmboxProduct.SelectedItem = ""
                    txtQuantity.Text = ListBoxCart.Text = ""

                    Exit Sub
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try

        Next

    End Sub

    Private Sub btnClearCart_Click(sender As Object, e As EventArgs) Handles btnClearCart.Click

        ListBoxCart.Items.Clear()

    End Sub

    Private Sub cmboxProduct_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmboxProduct.SelectedIndexChanged

        If cmboxProduct.SelectedItem = "iPad Case" Then
            txtPriceEach.Text = iPadCasePrice
        End If

        If cmboxProduct.SelectedItem = "iPad 2" Then
            txtPriceEach.Text = iPad2Price
        End If

    End Sub
End Class

首先添加一个属性作为总数

Dim Total As Decimal

然后修改btnAddToCart_ClickbtnClearCart_Click更新它(我假设显示total的文本框的名称是txtTotal

Private Sub btnAddToCart_Click(sender As Object, e As EventArgs) Handles btnAddToCart.Click
    If cmboxProduct.SelectedItem = "" Or txtQuantity.Text = "" Then
        MsgBox("Please fill out all required fields", MsgBoxStyle.OkOnly, "Invalid entered data")
    Else
        ListBoxCart.Items.Add("x" & txtQuantity.Text & " " & cmboxProduct.SelectedItem & " " & txtPriceEach.Text)

        Dim q = Decimal.Parse(txtQuantity.Text)
        Dim p = Decimal.Parse(txtPriceEach.Text.Remove(0, 1)) 'need to remove the $ before parse
        Total += p * q
        txtTotal.Text = String.Format("${0}", Total)

        'txtShoppingCart.AppendText("x" & txtQuantity.Text & " " & Product1 & Environment.NewLine)
    End If
End Sub

Private Sub btnClearCart_Click(sender As Object, e As EventArgs) Handles btnClearCart.Click

    ListBoxCart.Items.Clear()
    Total = 0
    txtTotal.Text = String.Empty

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一个熊猫数据框中的所有小时值与另一个数据框中的年值相乘?

python:如何将列表中的每个值与另一个列表中的所有值相乘

如何只允许将一个文件放入列表框控件C#

如何将一个列表与一个numpy数组相乘以获得一个多维数组?

如何将列表框项添加到VBA中的列?

如何将选定的列表框值从一个列表框发送到ASP.NET MVC中的另一个

如何将一个数据框列与一个列表中的多个数据框合并?

将一个列表框中的最后一个值添加/删除到另一个列表框时,整个列表框会清除吗?

如何将一个数据框(或矩阵)中的列与另一个数据框的列一一相乘并求和?

如何将一个列表框中的多个选定项移回另一个列表框?

如何将整个行与另一个数据框中的匹配行名相乘?

如何将数据框中的特定列与同一数据框中的一个特定列相乘?

如何将一个项目添加到列表框的顶部

如何将一个列表框从另一个列表框绑定到SelectedItem的XPath?

如何将多个选定的列表框项添加到另一个列表框

如何将多个列表框值分配给一个变量,然后将其放在“电子邮件到”字段中

vb:如何一次选择一个列表框中的多个项目?

将项目从一个列表框移动到另一个列表框时,如何添加到进入第二个列表框的字符串中?

如何将selectedItem从不同的列表框绑定到一个文本框?

如何将一个列表框中的项目添加到另一个列表框中?

如何将“ x”个列表框项目分组并添加到另一个列表框?

如何使用C#将一个或多个值从一个列表框移动到另一个列表框

如何检查列表框中的任何项目是否与另一个列表框匹配

如何将列表框数据添加到另一个网页上的文本框。使用 Visual Studio 2015 C#

如何将列表框项保存为 .config 文件并将其读回列表框?

如何从另一个列表框更新列表框

如何将项目添加到 vb.net 中的另一个列表框

如何将列表框添加到循环中的列表框列表中?

如何将列表框中的选定项值从一个模块返回到另一个模块?