许多项的小计计算 - Visual Basic

时间:2015-03-22 20:14:57

标签: vb.net

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

我的问题是:如果用户在多个项目文本框中输入数量,我该怎么做?我被困了,请帮忙。

左侧有一个标签,右侧有一个文本框。标签上写着“衬衫(10.00美元)”,文本框是他们输入他们购买的衬衫数量的地方。对于其他3个标签旁边的其他3个文本框也是如此,它们输入他们购买的商品的数量。然后他们在选择运输后点击计算,它给了他们一个小计和所有其余的。除了获得小计所需的东西之外,不要打扰运费或其他任何东西。我需要这样做,以便用户可以在一个,两个,三个或所有文本框中输入数量并点击计算以获得正确的小计。对不起,如果我第一次没有正确解释。这就是我所需要的,一种让计算正常工作的方法。请帮忙! :C

2 个答案:

答案 0 :(得分:0)

根据完整编辑的问题编辑回答:

我已删除,并添加了一些内容。 不要忘记添加运费。 而且不要只是复制粘贴这个。研究它,看看它是如何工作的,然后调整你的代码。

但是,如果没有过于复杂的事情,最简单的方法是分别对每个产品求和,然后将它组合起来。

看看这是否是您正在寻找的。 *注意。毕竟,如果你只是在文本框中指定“0”而不是使用Catch ex,那就更好了。

 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

您的代码有点混乱......我只对按钮点击事件进行了更改。

答案 1 :(得分:0)

这是一个简单的订购系统,它可以简单地简化计算,只需在订单中添加/删除产品即可。

示例:

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

与订单相关的类型:

Public Class ProductOrder
    Property Product() As Product
    Property Quantity() As Integer

    ReadOnly Property Total As Decimal
        Get
            Return Product.Price*Quantity
        End Get
    End Property


    Public Sub New(ByVal product As Product, ByVal quantity As Integer)
        Me.Product = product
        Me.Quantity = quantity
    End Sub
End Class

Public Class Order
    Private ReadOnly _products As List(Of ProductOrder)

    Public Sub New()
        _products = New List(Of ProductOrder)()
    End Sub

    ReadOnly Property Products() As List(Of ProductOrder)
        Get
            return _products
        End Get
    End Property

    ReadOnly Property GrandTotal() As Decimal
        Get
            Return Products.Sum(Function (x) x.Total)
        End Get
    End Property
End Class

<强> TODO

  • 以友好的方式呈现此内容,例如使用DataGrid
  • 允许用户使用Product或其他
  • 添加ComboBox
  • 等...

请注意,由于您没有告诉您使用的是什么框架(WPF或表格),我还没有解决这些问题。

祝你好运!