我的错误捕获功能有什么问题?

时间:2015-05-21 21:16:53

标签: vb.net function

我有一个购买衬衫的程序,我正在更改它,所以错误捕获通过一个函数发生,所以我删除了我以前的错误捕获,创建了该函数,并尝试了它。但在某些地方我必须搞砸,因为它不能正常工作。我一步一步地跟着班上的视频指南,但我找不到错误的地方。我一直试图弄清楚几个小时但却无处可去。任何帮助,将不胜感激。发生了什么事情,当我点击计算或下一个销售时,它总是会给我我的错误消息“请输入一个有效的数字”我尝试输入0,1,25,50,51,55和5a。它应该给我一个错误信息5a说“请输入一个有效的数字”,因为它不是数字。对于0,51和55,它应该给我错误消息“请输入1到50之间的数字”。我甚至无法收到该错误消息,因为我输入的每个号码都给出了“请输入有效数字” “ 错误信息。我哪里做错了?请帮我找到错误。

这是功能:

    Private Function CheckTextBox(ByVal TextBoxName As TextBox, ByVal Min As Single, ByVal Max As Single, ByVal IntCheck As Boolean) As Boolean

    If Not IsNumeric(TextBoxName) Then
        MessageBox.Show("Please enter a valid number", "Error", MessageBoxButtons.OK)
        TextBoxName.Text = ""
        TextBoxName.Focus()
        Exit Function
    End If

    If Val(TextBoxName.Text) < Min Or Val(TextBoxName.Text) > Max Then
        MessageBox.Show("Please enter a number from " & Min & " to " & Max, "Error", MessageBoxButtons.OK)
        TextBoxName.Text = ""
        TextBoxName.Focus()
        Exit Function
    End If

    If IntCheck Then
        If Val(TextBoxName.Text) <> Int(TextBoxName.Text) Then
            MessageBox.Show("Please enter a whole number", "Error", MessageBoxButtons.OK)
            TextBoxName.Text = ""
            TextBoxName.Focus()
            Exit Function
        End If
    End If

    Return True

End Function

以下是“计算”按钮的代码:

    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

    If CheckTextBox(txtQuantity, 1, 50, True) = False Then
        Exit Sub
    End If

    intQuantity = CInt(txtQuantity.Text)   'not sure if this line is needed?


    '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 btnNextItem_Click(Sender As Object, e As EventArgs) Handles btnNextItem.Click
    'Local Variable Declaration Section
    Dim sngSalesTax As Single
    Dim intQuantity As Integer
    Dim sngItemTotal As Single
    'Data Input Section

    If CheckTextBox(txtQuantity, 1, 50, True) = False Then 'Uses the function to error trap exits sub if functions returns as false
        Exit Sub
    End If

    intQuantity = CInt(txtQuantity.Text)

        '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
        'Output section
        lblShowShippingCost.Text = ""
        lblShowOrderTotal.Text = ""
        lblShowSubTotal.Text = FormatCurrency(msngSubtotal)  'Displays the Sub Total
        lblShowSalesTax.Text = FormatCurrency(sngSalesTax)  'Displays the Sales Tax
End Sub

0 个答案:

没有答案