validating input controls

O

Osmosis

I have a form with several controls, which all have their validating and
validated events in my code.

However, if these controls don't get focus, these events don't get called.

When my OK button is pressed, I need all the controls to be validated.

How can I get this done ? Do I have to call the events individually for
each control, and if so, how do I do this ?

Thx,
Peter
 
J

Jan Nielsen

Hi Peter
I have just solved this problem myself.
I am not sure though that this is a smart solution but it works.
********************
' form Level

Dim NyPostValideret As Boolean ' Has the newly added record had its controls
validated

Dim KontrolValideret As Boolean ' Has the single control been validated



'I have only showed the NextRecord as an example.

Private Sub btnNaestePost_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNaestePost.Click

Try

If NyPostValideret = False Then

If ValidateAll() = False Then Exit Sub ' Data is missing

End If



' Add a new record

Private Sub btnNyPost_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNyPost.Click

Try

' Code for adding a record

..

DsBoerneKirken1_PositionChanged(False)

NyPostValideret = False



Private Function ValidateAll() As Boolean

Dim e As System.ComponentModel.CancelEventArgs = New
System.ComponentModel.CancelEventArgs()

combokoen_Validating(CboKoen, e)

If KontrolValideret = False Then

ValidateAll = False

Exit Function

End If

txtFornavn_Validating(txtFornavn, e)

If KontrolValideret = False Then

ValidateAll = False

Exit Function

End If

txtPostnummer_Validating(txtPostnummer, e)

If KontrolValideret = False Then

ValidateAll = False

Exit Function

End If

cbogruppe_Validating(CboGruppe, e)

If KontrolValideret = False Then

ValidateAll = False

Exit Function

End If

CboMedlemsStatus_Validating(CboMedlemsStatus, e)

If KontrolValideret = False Then

ValidateAll = False

Exit Function

End If



KontrolValideret = True

NyPostValideret = True

Return True



End Function





Sub ValidateNumeric(ByVal ctlControl As Control)

If Not IsNumeric(ctlControl.Text) Then

Throw New Exception("Indtast venligst et tal")

Else

ErrorProvider1.SetError(ctlControl, "") ' clear the error

End If

End Sub



Sub ValidateNotBlank(ByVal ctlControl As Control)

If ctlControl.Text = "" Then

Throw New Exception("Dette felt skal have en værdi")

Else

ErrorProvider1.SetError(ctlControl, "")

End If

End Sub




' Showing 2 examples of controls

Private Sub combokoen_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles CboKoen.Validating

Try

ValidateNotBlank(CboKoen)

KontrolValideret = True

Catch ex As Exception

e.Cancel = True

ErrorProvider1.SetError(CboKoen, ex.Message)

KontrolValideret = False

End Try

End Sub



Private Sub txtPostnummer_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles txtPostnummer.Validating

Try

ValidateNumeric(txtPostnummer)

Catch ex As Exception

e.Cancel = True

ErrorProvider1.SetError(txtPostnummer, ex.Message)

End Try

End Sub





********************************


Best regards

Jan
 
T

Tom Leylan

Osmosis said:
I have a form with several controls, which all have their validating and
validated events in my code.
How can I get this done ? Do I have to call the events individually for
each control, and if so, how do I do this ?

Peter: I probably won't explain this too well but the problem seems to be
that your data validation code is contained in the control's event handler.

If for instance you require a US State abbreviation to be entered in a
textbox named txtState. It isn't the job of the textbox to validate that
input but rather that of a USState object behind it. You can put the
"brains" into the controls but then you have controls chatting with other
controls in situations where selecting "US" requires one set of validation
rules and selecting "Foreign" requires another set. You run into cases
where if they choose option "5" then textbox4 and textbox5 needs to be
blank.

If you instead put the validation logic into an object you could simply ask
that object if it is valid... you don't have to enter/exit a control or fire
an event handler to get it to happen.

If the address object needs a name, address, city, state, zip and phone
number and one of them is missing the object can report it... you don't ask
the textbox if it knows what the phone number is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top