因此,我正在编写一个程序,您可以在其中从组合框中选择产品,选择数量,然后它将在列表框中返回价格和数量。
但是,我希望能够将每行产品的价格乘以数量,然后将所有行的成本相加得出“总成本”。我该怎么做?
形式如下:
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_Click
和btnClearCart_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] 删除。
我来说两句