【发布时间】:2025-11-26 06:25:01
【问题描述】:
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
【问题讨论】:
标签: vb.net