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
 
Don't use Labels. That's exactly what the ErrorProvider component is for. You set an error string for a particular control and the ErrorProvider displays an icon beside it. Clear the error string and the icon disappears. When the user mouses over the icon the error string is displayed in a tool tip.

As for your question, that's exact;y what your code is telling it to do. By using ElseIf you are specifically tell in it to test the second condition if and only if the first condition is not True. As soon as a condition evaluates to True all the remaining conditions are skipped. If you want all the conditions to be evaluated then you need to use a separate If block for each one.
 
an erro provider sounds interesting... Do you think you could show me an example of how to set it up for one of my fields??

I found a snippet of code but am unsure what the errorprovider1 at the start is. do i need to reference it somewhere as it is throwing an error

VB.NET:
        Dim CarNoErrorProvider As System.Windows.Forms.ErrorProvider
        If txtCarNo.Text = "" Then
            CarNoErrorProvider.SetError(txtCarNo, "Cannot leave textbox blank")
        Else
            CarNoErrorProvider.SetError(txtCarNo, "")
        End If
Using this but get the following error code--

Additional information: Object reference not set to an instance of an object.
 
Last edited:
The ErrorProvider is a component and it is available from the Toolbox. You add one to your form in the design window like other controls and components. You then use code like you posted to set or clear the error for any control you like. You simply pass the control to the first argument, which is txtCarNo in your example.
 
How do you mean 'pass the control' to txtCarNo. I have added the control to the form, named it CarNoErrorProvider and have the same code but it still shows the same error - System.null reference exception
 
Last edited:
are you still using this line......?

VB.NET:
Dim CarNoErrorProvider As System.Windows.Forms.ErrorProvider

If you are delete it. If you've added it to your form already then you dont need it. If you want to create an instance of a class and be able to use it then you have to make a 'NEW' instance of it, or pass it a reference to an existing one.

VB.NET:
Dim CarNoErrorProvider As NEW System.Windows.Forms.ErrorProvider
 
Ah right, thats what it was. thankyou very much, i'll remember that. It seems to work well now, just one thing though, it flags up the errors fine but once ive corrected the errors (ie filled in txtCarNo) the error icon doesn't disapear from the screen

VB.NET:
    Public Sub validateForm2()

        Dim CarNoErrorProvider As New System.Windows.Forms.ErrorProvider
        If txtCarNo.Text = "" Then
            CarNoErrorProvider.SetError(txtCarNo, "Cannot leave textbox blank")
        Else
            CarNoErrorProvider.SetError(txtCarNo, "")
        End If

        Dim SupplierErrorProvider As New System.Windows.Forms.ErrorProvider
        If cboSupplier.SelectedValue = (0) Then
            SupplierErrorProvider.SetError(cboSupplier, "Cannot leave textbox blank")
        Else
            SupplierErrorProvider.SetError(cboSupplier, "")
        End If

    End Sub

Do i need some sort of clear error command in the else??

CarNoErrorProvider.clearError()
 
say's 'clear is not a member of System.Windows.Forms.ErrorProvider'

i also tried .icon.dispose which didnt wrk
 
Aren't i already doin that in the else?

SupplierErrorProvider.SetError(cboSupplier, "")

Do you think i need the 2.0 framework? will that make a difference to other things ive already used?
 
you don't need to upgrade anything, and you dont need to .net framework 2.0 either thats for vs.net 2005.
Setting argument to an empty string should have worked. My guess is that something else is wrong here. Have you stepped through the code to see if that line is executing? i'd try that and see what happens.
 
I just did step through - it is definatly hitting the else statement when i go back and fill in txtCarNo. The icon is still displayed!! damn this is annoying ;-)

Any other ideas vis, much apreciate your effort
 
Fact remains that it should have worked. Are you sure that the control you are passng in to error providers first argument, is the control you are setting the error for. If it is i'm at a bit of a loss. I'll have to try it out when i get home in about an hour or so.
 
Back
Top