I am trying to validate two text boxes in VB 10 Express. The first validation (parts) is working as desired, and the form will not proceed to the next step until zero or a positve number is entered. The second validation (labor) is not working as I would like. If left blank or a negative number or text is entered, the message box displays "Labor must be zero or a positive number", but when you click ok, the remainder of the form executes. I would like to force the user to enter zero or a positive number before moving on. I can't figure out why the two validations are behaving differently. Any advice?

        'Get the parts cost from user.
        If Decimal.TryParse(txtParts.Text, decParts) Then
            'Validate the parts costs.
            If decParts >= 0 Then
                'display parts
                lblParts.Text = decParts.ToString("c")


                'Get the labor cost from user
                If Decimal.TryParse(txtLabor.Text, decLabor) Then
                    'Validate the labor cost.
                    If decLabor >= 0 Then
                        'add labor to the hidden service label
                        lblLabor.Text = decLabor.ToString("c")
                    Else
                        'Error:  labor prices are missing or negative
                        MessageBox.Show("Labor must be 0 or a positive number.")
                    End If
                Else
                    'Error: labor prices are missing or negative.
                    MessageBox.Show("Labor must be 0 or a positive number.")
                End If

                'Get the parts tax
                decTax = decParts * decTAX_RATE

                decServiceLabor = OilLube() + Flushes() + Misc() + decParts + decLabor
                decTotal = decServiceLabor + decParts + decTax

                lblServiceLabor.Text = decServiceLabor.ToString("c")
                lblTax.Text = decTax.ToString("c")
                lblTotal.Text = decTotal.ToString("c")

            Else
                'Error: parts prices are missing or negative
                MessageBox.Show("Parts must be 0 or a positive number.")
            End If
        Else
            'Error: parts prices are missing or negative.
            MessageBox.Show("Parts must be 0 or a positive number.")
        End If

Recommended Answers

All 3 Replies

You could just put Exit Sub or Function when you get to the Else after the Error Message. You should break them up into 2 different Functions, A Labor Function and A Parts Price Function Return A Boolean and then when both Return True Execute the rest of your code.

There are several options here.

One option would be to use a masked text box. It is much easier to use than it sounds and the validation is done for you right in the text box. There is even a member that will validate according to a specific type of number.

Also you could use the text changed event to check for non-numeric entries then only enable the button when each one has valid entries. A couple of boolean variables would work for that.

A re-write of your existing code can work. Perhaps something like this:

Private Function ValidatInput(Input As String, ByRef Result As Decimal, ByRef Output As Label) As Boolean
    Dim Validate as Boolean = False
    If Decimal.TryParse(Input, Result) Then
        'Validate the cost.
        If Result >= 0 Then
            'display
            Output.Text = Result.ToString("c")
            Validate = True
        End If
    End If
    ValidateInput = Validate
End Function

Dim ValidParts As Boolean = ValidateInput(txtParts.Text, decParts, lblParts)
Dim ValidLabor As Boolean = ValidateInputtxtLabor.Text, decLabor, lblLabor)

If ValidParts And ValidLabor Then
    'Get the parts tax
    decTax = decParts * decTAX_RATE
    decServiceLabor = OilLube() + Flushes() + Misc() + decParts + decLabor
    decTotal = decServiceLabor + decParts + decTax
    lblServiceLabor.Text = decServiceLabor.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = decTotal.ToString("c")
End If    
If Not ValidParts Then
    'Error: parts prices are missing or negative.
    MessageBox.Show("Parts must be 0 or a positive number.")
End If
If Not ValidLabor Then
    'Error: labor prices are missing or negative
    MessageBox.Show("Labor must be 0 or a positive number.")
End If

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.