windows form validation

sharc

Member
Joined
Jul 11, 2005
Messages
11
Programming Experience
3-5
Can anyone point me in the direction to how I would go about validating windows forms? :confused:
 
sorry :D
I've got a form set up in windows, with text fields for names, dates, email addresses etc set up. ... before clicking on the submit button, I'd like to validate that the data that's trying to be submitted is in the proper format .. .. dates are dates, email addresses have no spaces, but have an @ sign etc.. . just like how it's done for web forms. .. .
so on click of the submit button, maybe if I could have an exclamation point come up near the field, or just some sort of alert that will inform the user what they've done wrong,and once these checks are satisfied, then the form will then be able to be submitted.

Can this be done? Please help!
 
Dates should not be an issue at all because you should be using DateTimePicker controls, which do the validation for you. To validate e-mail address format you should use a regular expression via a Regex object. There are numerous examples of patterns suitable for e-mail address validation on the Web. To indicate an invalid filed you should use an ErrorProvider component.

I would strongly recommend not waiting until the user presses the Submit button to validate. It always annoys me when I press a button and then get told I've done something wrong when I could have been told before. Do your validation as the user enters the data and don't even enable the Submit button until all the data is valid.
 
Validate on lost focus event of each control.
No, don't. Firstly, you should very, very rarely use the LostFocus event at all, as the documentation says. You should use the Leave event in preference. That said, validation should be performed on the Validating event.
 
I did it by adding an error provider to my form from the tool box and in the validating event of the control i want to validate use something like this:

VB.NET:
 Private Sub Address2Txt_Validating(ByVal sender As System.Object, _
 ByVal e As System.ComponentModel.CancelEventArgs) _
Handles Address2Txt.Validating

If CType(sender, TextBox).Text = "" Then
            ErrorProvider1.SetError(sender, _
             "You must enter an invoice address line 2.")
Else
            ErrorProvider1.SetError(sender, "")
End If
End Sub

Hope this helped (or at least partially).

Alex
 
Last edited:
Back
Top