Reputation: 1
Could someone show some guidance(am not asking to do my homework) with validating a form with multiple text boxes? User would be informed what was the problematic field.
The source of the form:
Private Sub btnNewUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewUser.Click
'If txtEmail.Text.Contains(" "c) Or Not(InStr(txtEmail.Text, "@")) Then
'txtEmail.Clear()
'ElseIf txtPassword.Text.Contains(" "c) Then
'txtPassword.Clear()
'ElseIf txtPIN.Text ''#uh
aryUserRecord(0) = txtEmail.Text
aryUserRecord(1) = txtPassword.Text
aryUserRecord(2) = txtPIN.Text ''#consists of a letter then two numbers then another addNewUser = Join(aryUserData, ",")
''#more source
Me.DialogResult = DialogResult.OK
End Sub
Upvotes: 0
Views: 4634
Reputation: 4801
You can use an ErrorProvider to mark the problematic fields. You'll want to hook up with the validating event for each TextBox. Something like this:
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, "Text cannot be empty")
e.Cancel = True
End If
End Sub
Then when the Textbox does actually validate, you can hook up to the Validated event to clear out the ErrorProvider:
Private Sub TextBox1_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
ErrorProvider1.SetError(TextBox1, "")
End Sub
Upvotes: 5
Reputation: 335
Try reading up on RegularExpressionValidator.
You could have one assigned for each textbox to validate user input client side using regular expressions, which based on your comments to the question seem like a good choice.
with winforms you'll need to implement Validating and Validated events
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx
in the link above an example is provided for e-mail. This should give you a reference to start
Upvotes: 0