为什么我的错误陷阱发生了? Visual Basic

时间:2015-03-23 03:24:18

标签: vb.net

所以我想知道为什么当我点击“下一个项目”按钮时它告诉我"请输入有效数量"即使我在文本框中输入了1。当我点击计算时,没有错误。

为什么它会告诉我单击“下一个项目”按钮后我还没有输入有效数量?

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
Dim msngItemPrice As Single = -1
Dim msngSubtotal As Single
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    'Local Variable Declaration Section
    Dim sngSalesTax As Single
    Dim sngOrderTotal As Single
    Dim intQuantity As Integer
    Dim sngItemTotal As Single
    '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 = CInt(txtQuantity.Text)
    Catch ex As Exception
        MessageBox.Show("Please enter a valid quantity.", "Error", MessageBoxButtons.OK)    'Displays an error messsage when there is an invalid number
        txtQuantity.SelectAll()  'Selects the text
        txtQuantity.Focus()    'Puts the cursor in the quantity textbox
        Exit Sub    'Terminates the click event to allow valid input.
    End Try



    'Calculation Section
    sngItemTotal = msngItemPrice * intQuantity
    msngSubtotal += sngItemTotal     'Calculates the subtotal
    sngSalesTax = msngSubtotal * csngSalesTaxRate    'Calculates Sales Tax based on the sales tax rate constant
    sngOrderTotal = msngSubtotal + 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(msngSubtotal)  'Displays the Sub Total
    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, btnNextItem.Click, btnNextSale.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
    txtQuantity.Text = "" 'Clears the Quantity text box
    txtQuantity.Focus()   'Puts the cursor in the Quantity 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
    txtQuantity.Text = "" 'Clears the Quantity text box
    txtQuantity.Focus() 'Puts the cursor in the Quantity 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

Private Sub btnNextSale_Click(sender As Object, e As EventArgs) Handles btnNextSale.Click
    radShirts.Checked = True
    radShirts.Checked = False
    radGround.Checked = True
    radGround.Checked = False
    msngSubtotal = 0
    msngItemPrice = -1
    msngShippingCost = -1
    lblShowSubTotal.Text = ""
    lblShowShippingCost.Text = ""
    lblShowOrderTotal.Text = ""
    txtQuantity.Text = ""
    txtQuantity.Focus()

End Sub

Private Sub radShirts_CheckedChanged(sender As Object, e As EventArgs) Handles radShirts.CheckedChanged
    msngItemPrice = 10
End Sub

Private Sub radPremiumShirts_CheckedChanged(sender As Object, e As EventArgs) Handles radPremiumShirts.CheckedChanged
    msngItemPrice = 20
End Sub

Private Sub radHats_CheckedChanged(sender As Object, e As EventArgs) Handles radHats.CheckedChanged
    msngItemPrice = 15
End Sub

Private Sub radStickers_CheckedChanged(sender As Object, e As EventArgs) Handles radStickers.CheckedChanged
    msngItemPrice = 5
End Sub
Private Sub btnNextItem_Click(Sender As Object, e As EventArgs) Handles btnNextItem.Click
    'Local Variable Declaration Section
    Dim sngSalesTax As Single
    Dim sngOrderTotal As Single
    Dim intQuantity As Integer
    Dim sngItemTotal As Single
    'Data Input Section

    Try     'Checks to see if the quantity is a valid number
        intQuantity = CInt(txtQuantity.Text)
    Catch ex As Exception
        MessageBox.Show("Please enter a valid quantity.", "Error", MessageBoxButtons.OK)    'Displays an error messsage when there is an invalid number
        txtQuantity.SelectAll()  'Selects the text
        txtQuantity.Focus()    'Puts the cursor in the quantity textbox
        Exit Sub    'Terminates the click event to allow valid input.
    End Try



    'Calculation Section
    sngItemTotal = msngItemPrice * intQuantity 'Calculates item total
    msngSubtotal += sngItemTotal     'Calculates the subtotal
    sngSalesTax = msngSubtotal * csngSalesTaxRate    'Calculates Sales Tax based on the sales tax rate constant
    sngOrderTotal = msngSubtotal + 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(msngSubtotal)  'Displays the Sub Total
    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
End Sub
End Class 

1 个答案:

答案 0 :(得分:1)

btnNextItem.Click有两个事件处理程序:

Private Sub btnClearCurentSale_Click(sender As Object, e As EventArgs) Handles btnClearCurentSale.Click, btnNextItem.Click, btnNextSale.Click

Private Sub btnNextItem_Click(Sender As Object, e As EventArgs) Handles btnNextItem.Click

在btnClearCurrentSale_Click中,将txtQuantity设置为"" 。这是在CInt之前执行的,因此当您尝试将其转换为整数时,txtQuantity.text始终为空。

你应该从btnClearCurentSale的句柄部分删除btnNextItem.click。