Form Validation

banks

Well-known member
Joined
Sep 7, 2005
Messages
50
Programming Experience
Beginner
Hi,

I need to validate my form so that when a user tries to insert a record, certain fields must be completed. If a field isn't filled, i need the label next to it to be highlighted red. I think i'm nearly there but it isnt working quite how i'd like.

At the mo, it does the first if, then skips the rest and goes to the msgbox part - i need it to loop round i think so that it goes through and checks all the fiellds before displaying the message and highlighting each's label red.

VB.NET:
    Public Sub validateForm()

        If Me.cboSupplier.SelectedValue = (0) Then
            lblSupplier.ForeColor = System.Drawing.Color.Red
        ElseIf cboMaterial.SelectedValue = (0) Then
            lblPartNo.ForeColor = System.Drawing.Color.Red
        ElseIf txtDefectDesc Is "" Then
            lblDefectDesc.ForeColor = System.Drawing.Color.Red
        ElseIf cboDefect.SelectedValue = (0) Then
            lblDefect.ForeColor = System.Drawing.Color.Red
        ElseIf cboAction.SelectedValue = (0) Then
            lblAction.ForeColor = System.Drawing.Color.Red
        End If

        'MsgBox("The fields in red must be updated")
        Exit Sub


    End Sub

What do i need to do to get this working please?

Al
 
You shouldn't be creating any ErrorProviders in code. If you added one to the form and that's the only one you need. You certainly don't create them in a method. Do you create a new TextBox every time you want to set its Text property? No you don't. You add a TextBox to the form in the designer and then you set the Text property of that object in code. It's the same with the ErrorProvider. You add one to the form in the designer and then you call the SetError method of that object in code.

The problem is that you're creating a new ErrorProvider every time, so if you pass an empty string to its SetError method you haven't cleared the error of the ErrorProvider you created last time.
 
Spot on, well spotted. Thanks for the explanation, i understand it properly now. Thanks for all the help guys, glad we got there :)

Al
 
Back
Top