Vb.net 多个项目的小计计算-Visual Basic
我的问题是:要让用户在多个条目文本框中输入数量,我该怎么做?我卡住了,请帮帮我Vb.net 多个项目的小计计算-Visual Basic,vb.net,Vb.net,我的问题是:要让用户在多个条目文本框中输入数量,我该怎么做?我卡住了,请帮帮我 左边是一个标签,右边是一个文本框。标签上写着“衬衫($10.00)”,文本框是他们输入购买衬衫数量的地方。其他3个标签旁边的其他3个文本框也是如此,它们输入他们正在购买的物品的数量。然后,他们点击计算后,选择航运和它给了他们一个小计和所有的休息。除了要得到小计所需的费用外,不要为运输或其他任何事情操心。我需要这样做,用户可以在一个、两个、三个或所有文本框中输入一个数量,然后点击calculate以获得适当的小计。对不
左边是一个标签,右边是一个文本框。标签上写着“衬衫($10.00)”,文本框是他们输入购买衬衫数量的地方。其他3个标签旁边的其他3个文本框也是如此,它们输入他们正在购买的物品的数量。然后,他们点击计算后,选择航运和它给了他们一个小计和所有的休息。除了要得到小计所需的费用外,不要为运输或其他任何事情操心。我需要这样做,用户可以在一个、两个、三个或所有文本框中输入一个数量,然后点击calculate以获得适当的小计。对不起,如果我第一次解释不正确。这就是我所需要的,一种让计算正常工作的方法。请帮忙c编辑回答完整编辑的问题:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Local Variable Declaration Section
Dim sngShirtPrice As Single = 10
Dim sngPremiumShirtPrice As Single = 20
Dim sngHatsPrice As Single = 15
Dim sngStickersPrice As Single = 5
Dim sngSalesTax As Single
Dim sngOrderTotal As Single
Dim StickersQuantity As Integer
Dim sngStickersTotal As Single
Dim ShirtsQuantity As Integer
Dim sngShirtsTotal As Single
Dim HatsQuantity As Integer
Dim sngHatsTotal As Single
If txtShirts.Text = Nothing Then
txtShirts.Text = "0"
End If
If txtHats.Text = Nothing Then
txtHats.Text = "0"
End If
If txtStickers.Text = Nothing Then
txtStickers.Text = "0"
End If
ShirtsQuantity = txtShirts.Text
HatsQuantity = txtHats.Text
StickersQuantity = txtStickers.Text
'Calculation Section
sngShirtsTotal = ShirtsQuantity * sngShirtPrice
sngHatsTotal = HatsQuantity * sngHatsPrice
sngStickersTotal = StickersQuantity * sngStickersPrice
Dim TTotal As Single
TTotal = sngShirtsTotal + sngHatsTotal + sngStickersTotal
sngSalesTax = TTotal * csngSalesTaxRate 'Calculates Sales Tax based on the sales tax rate constant
sngOrderTotal = TTotal + sngSalesTax + msngShippingCost 'Calculates total for the sale
mintOrdersPlacedToday = mintOrdersPlacedToday + 1 'Calculates the number of orders placed today, adds one to the previous number
msngTotalOfOrdersToday = msngTotalOfOrdersToday + sngOrderTotal 'Calculates the Total of all the orders placed today
'Output section
lblShowSubTotal.Text = FormatCurrency(TTotal) 'Displays the Subtotal
lblShowSalesTax.Text = FormatCurrency(sngSalesTax) 'Displays the Sales Tax
lblShowOrderTotal.Text = FormatCurrency(sngOrderTotal) 'Displays the Order Total
lblShowOrdersPlacedToday.Text = mintOrdersPlacedToday 'Displays the Orders placed today
lblShowTotalOfOrders.Text = FormatCurrency(msngTotalOfOrdersToday) 'Displays the Total of the Orders placed today
lblShowShippingCost.Text = FormatCurrency(msngShippingCost) 'Displays the total of the shipping cost
End Sub
我已经删除,并添加了一些东西别忘了添加装运成本。
不要只是复制粘贴这个。研究它,看看它是如何工作的,然后调整你的代码
但在不使事情过于复杂的情况下,最简单的方法是分别对每个产品求和,然后将其合并
看看这是不是你要找的。
*注意。毕竟,最好是在文本框中指定“0”,而不是使用Catch-ex
Public Class Form1
'Modular Variable Declaration Section
Dim mintOrdersPlacedToday As Integer
Dim msngTotalOfOrdersToday As Single
Dim msngShippingCost As Single = -1
Const csngSalesTaxRate As Single = 0.0625
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Local Variable Declaration Section
Dim sngShirtPrice As Single = 10
Dim sngPremiumShirtPrice As Single = 20
Dim sngHatsPrice As Single = 15
Dim sngStickersPrice As Single = 5
Dim sngSubTotal As Single
Dim sngSalesTax As Single
Dim sngOrderTotal As Single
Dim intQuantity As Integer
'Data Input Section
If msngShippingCost = -1 Then
MessageBox.Show("Please choose a shipping method", "Error", MessageBoxButtons.OK) 'Displays an error messsage when no shipping method is chosen
Exit Sub 'Terminates the click event to allow shipping method to be chosen first
End If
Try 'Checks to see if the price is a valid number.
'If it is, then it is assigned to the quantity variable, if not, error message and the event is halted.
intQuantity = txtShirts.Text
Catch ex As Exception
MessageBox.Show("Please enter a valid number of Shirts.", "Error", MessageBoxButtons.OK) 'Displays an error messsage when there is an invalid number
txtShirts.Text = "" 'Clears the text box
txtShirts.Focus() 'Puts the cursor in the price textbox
Exit Sub 'Terminates the click event to allow valid input.
End Try
Try
Catch ex As Exception
End Try
'Calculation Section
sngSubTotal = intQuantity * sngShirtPrice 'Calculates the subtotal
sngSalesTax = sngSubTotal * csngSalesTaxRate 'Calculates Sales Tax based on the sales tax rate constant
sngOrderTotal = sngSubTotal + sngSalesTax + msngShippingCost 'Calculates total for the sale
mintOrdersPlacedToday = mintOrdersPlacedToday + 1 'Calculates the number of orders placed today, adds one to the previous number
msngTotalOfOrdersToday = msngTotalOfOrdersToday + sngOrderTotal 'Calculates the Total of all the orders placed today
'Output section
lblShowSubTotal.Text = FormatCurrency(sngSubTotal) 'Displays the Subtotal
lblShowSalesTax.Text = FormatCurrency(sngSalesTax) 'Displays the Sales Tax
lblShowOrderTotal.Text = FormatCurrency(sngOrderTotal) 'Displays the Order Total
lblShowOrdersPlacedToday.Text = mintOrdersPlacedToday 'Displays the Orders placed today
lblShowTotalOfOrders.Text = FormatCurrency(msngTotalOfOrdersToday) 'Displays the Total of the Orders placed today
lblShowShippingCost.Text = FormatCurrency(msngShippingCost) 'Displays the total of the shipping cost
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Dim result = MessageBox.Show(" Are you sure you want to exit?", "Are you sure?", MessageBoxButtons.YesNo) 'Shows a messagebox for the user asking if they want to exit the program and gives them options.
If result = DialogResult.Yes Then 'States that if the user clicks Yes, the program will close
Me.Close() 'Exits the program
End If
End Sub
Private Sub btnClearCurentSale_Click(sender As Object, e As EventArgs) Handles btnClearCurentSale.Click
'Clears the information from current sale and resets the form for the next sale
radPickup.Checked = True 'Checks the Pickup radio button
radPickup.Checked = False 'Unchecks the Pickup radio button
btnCalculate.Enabled = True 'Enables the Calculate button
msngShippingCost = -1 'Sets Shipping Cost to -1
lblShowShippingCost.Text = "" 'Clears the Shipping Cost label
lblShowOrderTotal.Text = "" 'Clears the Order Total label
lblShowSalesTax.Text = "" 'Clears the Sales Tax label
lblShowSubTotal.Text = "" 'Clears the Sub Total label
txtShirts.Text = "" 'Clears the Shirts text box
txtShirts.Focus() 'Puts the cursor in the Shirts text box
End Sub
Private Sub radPickup_CheckedChanged(sender As Object, e As EventArgs) Handles radPickup.CheckedChanged
msngShippingCost = 0 'Sets shipping cost as $0
lblShowShippingCost.Text = "Free" 'Sets Shipping Cost label to show $0
lblShowOrderTotal.Text = "" 'Clears the Order Total label
End Sub
Private Sub radGround_CheckedChanged(sender As Object, e As EventArgs) Handles radGround.CheckedChanged
msngShippingCost = 6.75 'Sets shipping cost as $6.75
lblShowShippingCost.Text = FormatCurrency(6.75, 2) 'Sets Shipping Cost label to show $6.75
lblShowOrderTotal.Text = "" 'Clears the Order Total label
End Sub
Private Sub radTwoDay_CheckedChanged(sender As Object, e As EventArgs) Handles radTwoDay.CheckedChanged
msngShippingCost = 12 'Sets shipping cost as $12
lblShowShippingCost.Text = FormatCurrency(12, 2) 'Sets Shipping Cost label to show $12
lblShowOrderTotal.Text = "" 'Clears the Order Total label
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
'Clears the information for everything on the form
txtShirts.Text = "" 'Clears the Shirts text box
txtShirts.Focus() 'Puts the cursor in the Shirts text box
lblShowSubTotal.Text = "" 'Clears the Sub Total label
lblShowSalesTax.Text = "" 'Clears the Sales Tax label
lblShowShippingCost.Text = "" 'Clears the Shipping Cost label
lblShowOrderTotal.Text = "" 'CLears the Order Total label
lblShowOrdersPlacedToday.Text = "" 'Clears the Orders Placed Today label
lblShowTotalOfOrders.Text = "" 'Clears the Total of Orders Today label
mintOrdersPlacedToday = 0 'Resets the counter
msngTotalOfOrdersToday = 0 'Resets the accumulator
End Sub
End Class
你的代码有点乱。。。我只对按钮单击事件进行了更改。这里是一个简单的订购系统,它大大简化了计算,因为您只需向订单中添加/删除产品即可
示例:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Local Variable Declaration Section
Dim sngShirtPrice As Single = 10
Dim sngPremiumShirtPrice As Single = 20
Dim sngHatsPrice As Single = 15
Dim sngStickersPrice As Single = 5
Dim sngSalesTax As Single
Dim sngOrderTotal As Single
Dim StickersQuantity As Integer
Dim sngStickersTotal As Single
Dim ShirtsQuantity As Integer
Dim sngShirtsTotal As Single
Dim HatsQuantity As Integer
Dim sngHatsTotal As Single
If txtShirts.Text = Nothing Then
txtShirts.Text = "0"
End If
If txtHats.Text = Nothing Then
txtHats.Text = "0"
End If
If txtStickers.Text = Nothing Then
txtStickers.Text = "0"
End If
ShirtsQuantity = txtShirts.Text
HatsQuantity = txtHats.Text
StickersQuantity = txtStickers.Text
'Calculation Section
sngShirtsTotal = ShirtsQuantity * sngShirtPrice
sngHatsTotal = HatsQuantity * sngHatsPrice
sngStickersTotal = StickersQuantity * sngStickersPrice
Dim TTotal As Single
TTotal = sngShirtsTotal + sngHatsTotal + sngStickersTotal
sngSalesTax = TTotal * csngSalesTaxRate 'Calculates Sales Tax based on the sales tax rate constant
sngOrderTotal = TTotal + sngSalesTax + msngShippingCost 'Calculates total for the sale
mintOrdersPlacedToday = mintOrdersPlacedToday + 1 'Calculates the number of orders placed today, adds one to the previous number
msngTotalOfOrdersToday = msngTotalOfOrdersToday + sngOrderTotal 'Calculates the Total of all the orders placed today
'Output section
lblShowSubTotal.Text = FormatCurrency(TTotal) 'Displays the Subtotal
lblShowSalesTax.Text = FormatCurrency(sngSalesTax) 'Displays the Sales Tax
lblShowOrderTotal.Text = FormatCurrency(sngOrderTotal) 'Displays the Order Total
lblShowOrdersPlacedToday.Text = mintOrdersPlacedToday 'Displays the Orders placed today
lblShowTotalOfOrders.Text = FormatCurrency(msngTotalOfOrdersToday) 'Displays the Total of the Orders placed today
lblShowShippingCost.Text = FormatCurrency(msngShippingCost) 'Displays the total of the shipping cost
End Sub
为这一切提供动力的类:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Local Variable Declaration Section
Dim sngShirtPrice As Single = 10
Dim sngPremiumShirtPrice As Single = 20
Dim sngHatsPrice As Single = 15
Dim sngStickersPrice As Single = 5
Dim sngSalesTax As Single
Dim sngOrderTotal As Single
Dim StickersQuantity As Integer
Dim sngStickersTotal As Single
Dim ShirtsQuantity As Integer
Dim sngShirtsTotal As Single
Dim HatsQuantity As Integer
Dim sngHatsTotal As Single
If txtShirts.Text = Nothing Then
txtShirts.Text = "0"
End If
If txtHats.Text = Nothing Then
txtHats.Text = "0"
End If
If txtStickers.Text = Nothing Then
txtStickers.Text = "0"
End If
ShirtsQuantity = txtShirts.Text
HatsQuantity = txtHats.Text
StickersQuantity = txtStickers.Text
'Calculation Section
sngShirtsTotal = ShirtsQuantity * sngShirtPrice
sngHatsTotal = HatsQuantity * sngHatsPrice
sngStickersTotal = StickersQuantity * sngStickersPrice
Dim TTotal As Single
TTotal = sngShirtsTotal + sngHatsTotal + sngStickersTotal
sngSalesTax = TTotal * csngSalesTaxRate 'Calculates Sales Tax based on the sales tax rate constant
sngOrderTotal = TTotal + sngSalesTax + msngShippingCost 'Calculates total for the sale
mintOrdersPlacedToday = mintOrdersPlacedToday + 1 'Calculates the number of orders placed today, adds one to the previous number
msngTotalOfOrdersToday = msngTotalOfOrdersToday + sngOrderTotal 'Calculates the Total of all the orders placed today
'Output section
lblShowSubTotal.Text = FormatCurrency(TTotal) 'Displays the Subtotal
lblShowSalesTax.Text = FormatCurrency(sngSalesTax) 'Displays the Sales Tax
lblShowOrderTotal.Text = FormatCurrency(sngOrderTotal) 'Displays the Order Total
lblShowOrdersPlacedToday.Text = mintOrdersPlacedToday 'Displays the Orders placed today
lblShowTotalOfOrders.Text = FormatCurrency(msngTotalOfOrdersToday) 'Displays the Total of the Orders placed today
lblShowShippingCost.Text = FormatCurrency(msngShippingCost) 'Displays the total of the shipping cost
End Sub
具有以下价格的产品:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim beers = new ProductOrder(New BeerProduct, 20)
Dim tacos = new ProductOrder(New TacosProduct, 100)
Dim order As New Order
order.Products.Add(beers)
order.Products.Add(tacos)
Dim totalForBeers = beers.Total
Dim totalForTacos = tacos.Total
Dim grandTotal = order.GrandTotal
End Sub
End Class
一些产品:
Public MustInherit Class Product
MustOverride ReadOnly Property Price() As Decimal
End Class
订单相关类型:
Public Class BeerProduct
Inherits Product
Public Overrides ReadOnly Property Price() As Decimal
Get
Return 10.0D
End Get
End Property
End Class
Public Class TacosProduct
Inherits Product
Public Overrides ReadOnly Property Price() As Decimal
Get
Return 5.0D
End Get
End Property
End Class
待办事项
- 以友好的方式呈现,例如使用
DataGrid
- 允许用户使用组合框添加产品
- 等等
祝你好运 你不应该在计算中使用Try-Catch。验证输入是一个更干净的解决方案。Taffs,非常感谢您的帮助。实际上,我有一个try-catch,我没有输入所有代码,只是输入了我遇到问题的部分,这是为了正确计算大量项目的小计。并且让用户能够一次输入多个项目的数量。我能让你用Skype或者别的什么跟我聊天吗?只是文字聊天。直到我彻底解决这个问题?我将不胜感激。另外,我忘了在顶部将dim intQuantity定义为整数。我已经更新了我的回复。我也认为简单地在文本框中输入“0”而不是抓住前男友会更好。你把我弄丢了。我在用表格。我将在一秒钟内发布整个代码。返回以获取对文章的编辑。