Vb.net 许多项目的计算部分
下面是发生的事情: 衬衫数量:(在此处输入购买的衬衫数量) 高级衬衫数量:“ 帽子数目:“ 贴纸数量:“ 用户在文本框中输入购买的号码 然后,他们在选择配送选项后单击“计算”按钮 “计算”按钮需要计算小计,然后有一个增值税标签、运费标签、订单总额标签 我陷入困境的地方是计算小计。在我的“计算”按钮的“计算”部分,我尝试对它进行编码,但我的大脑停止了工作,或者其他什么,因为我被卡住了,我一直无法理解它。它是如此简单 这是我到目前为止的代码。我们将非常感谢您的帮助。我知道,代码中有些部分丢失了,因为我被卡住了Vb.net 许多项目的计算部分,vb.net,Vb.net,下面是发生的事情: 衬衫数量:(在此处输入购买的衬衫数量) 高级衬衫数量:“ 帽子数目:“ 贴纸数量:“ 用户在文本框中输入购买的号码 然后,他们在选择配送选项后单击“计算”按钮 “计算”按钮需要计算小计,然后有一个增值税标签、运费标签、订单总额标签 我陷入困境的地方是计算小计。在我的“计算”按钮的“计算”部分,我尝试对它进行编码,但我的大脑停止了工作,或者其他什么,因为我被卡住了,我一直无法理解它。它是如此简单 这是我到目前为止的代码。我们将非常感谢您的帮助。我知道,代码中有些部分丢失了,因为
'Local Variable Declaration Section
Const csngSalesTaxRate As Single = 0.0625
Dim sngShirtPrice As Single = 10
Dim sngPremiumShirtPrice As Single = 20
Dim sngHatPrice As Single = 15
Dim sngStickerPrice As Single = 2.99
Dim sngSubTotal As Single
Dim sngSalesTax As Single
Dim sngOrderTotal As Single
Dim sngShippingCost As Single
Dim intShirts As Integer
Dim intHats As Integer
Dim intStickers As Integer
Dim intPremiumShirts As Integer
'Data Input Section
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.
sngShirtPrice = CInt(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 'Checks if the quantity is a valid number.
'If it is, then it is assigned to the quantity variable, if not, error message and event is halted.
sngPremiumShirtPrice = CInt(txtPremiumShirts.Text)
Catch ex As Exception
MessageBox.Show("Please enter a valid number of Premium Shirts.", "Error", MessageBoxButtons.OK) 'Displays an error messsage when there is an invalid number
txtPremiumShirts.Text = "" 'Clears the text box
txtPremiumShirts.Focus() 'Puts the cursor in the price textbox
Exit Sub 'Terminates the click event to allow valid input.
End Try
Try 'Checks if the quantity is a valid number.
'If it is, then it is assigned to the quantity variable, if not, error message and event is halted.
sngHatPrice = CInt(txtHats.Text)
Catch ex As Exception
MessageBox.Show("Please enter a valid number of Hats.", "Error", MessageBoxButtons.OK) 'Displays an error messsage when there is an invalid number
txtHats.Text = "" 'Clears the text box
txtHats.Focus() 'Puts the cursor in the price textbox
Exit Sub 'Terminates the click event to allow valid input.
End Try
Try 'Checks if the quantity is a valid number.
'If it is, then it is assigned to the quantity variable, if not, error message and event is halted.
sngStickerPrice = CInt(txtStickers.Text)
Catch ex As Exception
MessageBox.Show("Please enter a valid number of Stickers.", "Error", MessageBoxButtons.OK) 'Displays an error messsage when there is an invalid number
txtStickers.Text = "" 'Clears the text box
txtStickers.Focus() 'Puts the cursor in the price textbox
Exit Sub 'Terminates the click event to allow valid input.
End Try
'Calculation Section
sngSubTotal = 'Calculates the subtotal
sngSalesTax = sngSubTotal * csngSalesTaxRate 'Calculates Sales Tax by multiplying Subtotal by Sales Tax Rate
sngOrderTotal = sngSubTotal + sngSalesTax 'Calculates total for the sale
mintOrdersPlacedToday = mintOrdersPlacedToday + 1 'Calculates the number of orders placed today, adds one to the previous number
'Output section
lblShowSubTotal.Text = FormatCurrency(sngSubTotal) 'Displays the Sub Total
lblShowSalesTax.Text = FormatCurrency(sngSalesTax) 'Displays the Sales Tax
lblShowOrderTotal.Text = FormatCurrency(sngOrderTotal) 'Displays the Order Total
lblShowOrdersPlacedToday.Text = mintOrdersPlacedToday
lblShowOrderTotal.Text = sngOrderTotal
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
txtShirts.Text = "" 'Clears the Shirts text box
txtPremiumShirts.Text = "" 'Clears the Premium Shirts text box
txtHats.Text = "" 'Clears the Hats text box
txtStickers.Text = "" 'Clears the Stickers text box
txtShirts.Focus() 'Puts the cursor in the Shirts text box
End Sub
您可以使用
Integer.TryParse
简单地处理返回而不是异常,而不是捕获异常。您可以使用NumericupDowns
完全避免它,并使其更简单。我找不到小计var,所以…我需要知道在计算部分的sngSubTotal=之后应该放什么。sngSubTotal=sngShirtPrice*intShirts+sngPremiumShirtPrice*intPremiumShirts+snghattprice*intHats+sngStickerPrice*intStickers?