Some VB help please :)

Discussion in 'OT Technology' started by Slimmy, Feb 5, 2003.

  1. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    Some VB.NET help please :)

    I'm working on part 2 of my project which includes coding a calculate button to calculate totals for the basic form I've created. I have all of the calculating coding done and i'm trying to do Input validation using a Try, End Try error catch procedure. However, for some reason I can't get it to work. I had used the same exact code that I had done in the lab last week but on this project it doesn't seem to work and no matter what I put into the textboxes I want to validate, I get the message box. Can anyone tell me what might be wrong with it? I can provide the files or project folder if necessary. :)

    Code:
    Private Sub mnuCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCalculate.Click
           
            'Dimension the variables
    
            Dim intQuantity1 As Integer
            Dim intQuantity2 As Integer
            Dim intQuantity3 As Integer
            Dim decPrice1 As Decimal
            Dim decPrice2 As Decimal
            Dim decPrice3 As Decimal
            Dim decSubtotal As Decimal
            Dim decTax As Decimal
            Dim decTotal As Decimal
    
            'Convert input to numeric value and check for data entry errors.
    
            Try
                If Not IsNumeric(txtQuantity1.Text) Then
                    MessageBox.Show("Quantity entered is not numeric.")
                Else
                    intQuantity1 = CInt(txtQuantity1.Text)
                End If
    
                'Convert input to numeric data.
                intQuantity1 = CInt(txtQuantity1.Text)
                intQuantity2 = CInt(txtQuantity2.Text)
                intQuantity3 = CInt(txtQuantity3.Text)
                decPrice1 = CDec(txtPrice1.Text)
                decPrice2 = CDec(txtPrice2.Text)
                decPrice3 = CDec(txtPrice3.Text)
    
                'Perform calculations
    
                decSubtotal = intQuantity1 * decPrice1 + intQuantity2 * decPrice2 + _
                intQuantity3 * decPrice3
                decTax = decSubtotal * decTaxRate
                decTotal = decSubtotal + decTax
    
                'Display the calculation results
                lblSubtotalAmount.Text = FormatCurrency(decSubtotal)
                lblTaxAmount.Text = FormatCurrency(decTax)
                lblTotalAmount.Text = FormatCurrency(decTotal)
    
                'Catch data entry errors in quantity
    
            Catch MyErr As InvalidCastException
                MessageBox.Show("Quantity entered is not numeric.", _
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            End Try
    And yes, I realize that I have the convert code in there twice for Quantity1, but I put them all back there until I can figure this out, just so I don't have to type em out again. :)
     
    Last edited: Feb 5, 2003
  2. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    I basically have the project done, except for adding the Input Validation, I just want to do it for the 3 Quantity textboxes that the user will be inputting data into.
     
  3. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    What does txtQuantity1.Text evaluate to? I mean if that's where it's hanging then you're not testing the right information right?
     
  4. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    txtQuantity1.Text is the first quantity textbox. They're numbered 1-3.
     
  5. thewise1

    thewise1 Guest

    Without trying it myself...

    Simplest way to debug that is to simply popup a message box with the result of IsNumeric(txtQuantity1.text).

    If it's false, but you have numbers in the textbox, then it considers it a string variable that can't be converted to a double.

    If it's true... :confused: It shouldn't be :o

     
  6. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I realize that, what does it evaluate to

    In other words,

    txtquantity1.text = ?

    After you have entered a number into the control box.

    My guess is that what you think it evaluates to (a number) is in fact not a number. So when it does a check to see if it's IsNumeric, it returns false all the time. I use mostly VBA which is a close cousin and the syntax would simply be:

    IsNumeric(txtquantity1)

    without the .text
     
  7. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    Let me debug real quick and see what it says.
     
  8. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    Hmm, this is strange. I have the default values set for the price and the quantity textboxes to 0, and if I input numbers into all of them it works fine. I also somehow got the message box to stop popping up for the quantity, but now I get InvalidCastException errors for the price. Because they're strings that can't be converted to decimals. Hmmm.
     
  9. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    Alright, here's the full code if it helps.

    Code:
        'Dimension modular level variables and constants.
    
        Const decTaxRate As Decimal = 0.06D
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            
    
            'Exit the project
    
            Me.Close()
    
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            
    
            'Clear contents of TxtSubtotal, TxtTax, TxtDiscount, TxtTotal, TxtQuantity, TxtPrice and revert focus to cboProduct.
    
            txtDiscount.Text = ""
            txtQuantity1.Text = "0"
            txtPrice1.Text = ""
            txtQuantity2.Text = "0"
            txtPrice2.Text = ""
            txtQuantity3.Text = "0"
            txtPrice3.Text = ""
            txtProduct1.Text = ""
            txtProduct2.Text = ""
            txtProduct3.Text = ""
            txtProduct1.Focus()
            lblSubtotalAmount.Text = ""
            lblTaxAmount.Text = ""
            lblTotalAmount.Text = ""
    
        End Sub
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            
    
            'Dimension the variables
    
            Dim intQuantity1 As Integer
            Dim intQuantity2 As Integer
            Dim intQuantity3 As Integer
            Dim decPrice1 As Decimal
            Dim decPrice2 As Decimal
            Dim decPrice3 As Decimal
            Dim decSubtotal As Decimal
            Dim decTax As Decimal
            Dim decTotal As Decimal
    
            'Convert input to numeric data.
            intQuantity1 = CInt(txtQuantity1.Text)
            intQuantity2 = CInt(txtQuantity2.Text)
            intQuantity3 = CInt(txtQuantity3.Text)
            decPrice1 = CDec(txtPrice1.Text)
            decPrice2 = CDec(txtPrice2.Text)
            decPrice3 = CDec(txtPrice3.Text)
    
            'Perform calculations
    
            decSubtotal = intQuantity1 * decPrice1 + intQuantity2 * decPrice2 + _
            intQuantity3 * decPrice3
            decTax = decSubtotal * decTaxRate
            decTotal = decSubtotal + decTax
    
            'Display the calculation results
            lblSubtotalAmount.Text = FormatCurrency(decSubtotal)
            lblTaxAmount.Text = FormatCurrency(decTax)
            lblTotalAmount.Text = FormatCurrency(decTotal)
    
        End Sub
    
        Private Sub MenuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
           
    
            'Choose to exit project.
    
            Me.Close()
    
        End Sub
    
        Private Sub MenuClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClear.Click
           
    
            'clear all textboxes and calculated totals
    
            txtDiscount.Text = ""
            txtQuantity1.Text = "0"
            txtPrice1.Text = ""
            txtQuantity2.Text = "0"
            txtPrice2.Text = ""
            txtQuantity3.Text = "0"
            txtPrice3.Text = ""
            txtProduct1.Text = ""
            txtProduct2.Text = ""
            txtProduct3.Text = ""
            txtProduct1.Focus()
            lblSubtotalAmount.Text = ""
            lblTaxAmount.Text = ""
            lblTotalAmount.Text = ""
    
    
        End Sub
    
        Private Sub mnuCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCalculate.Click
            
    
            'Dimension the variables
    
            Dim intQuantity1 As Integer
            Dim intQuantity2 As Integer
            Dim intQuantity3 As Integer
            Dim decPrice1 As Decimal
            Dim decPrice2 As Decimal
            Dim decPrice3 As Decimal
            Dim decSubtotal As Decimal
            Dim decTax As Decimal
            Dim decTotal As Decimal
    
            'Convert input to numeric data.
    
            intQuantity1 = CInt(txtQuantity1.Text)
            intQuantity2 = CInt(txtQuantity2.Text)
            intQuantity3 = CInt(txtQuantity3.Text)
            decPrice1 = CDec(txtPrice1.Text)
            decPrice2 = CDec(txtPrice2.Text)
            decPrice3 = CDec(txtPrice3.Text)
    
            'Perform calculations
    
            decSubtotal = intQuantity1 * decPrice1 + intQuantity2 * decPrice2 + _
            intQuantity3 * decPrice3
            decTax = decSubtotal * decTaxRate
            decTotal = decSubtotal + decTax
    
            'Display the calculation results
    
            lblSubtotalAmount.Text = FormatCurrency(decSubtotal)
            lblTaxAmount.Text = FormatCurrency(decTax)
            lblTotalAmount.Text = FormatCurrency(decTotal)
    
        End Sub
    
    End Class
     
  10. Slimmy

    Slimmy Get Richt or Die Trying

    Joined:
    Mar 14, 2000
    Messages:
    135,671
    Likes Received:
    29
    Location:
    Ft. Lauderdale
    Nevermind, I figured it out! :big grin::big grin::big grin::big grin::big grin:

    :blue:
     

Share This Page