Validating Text Controls withan ErrorProviderUsing the ErrorProvider Control | | If you haven't read my quick piece on Validating text entries, you may want to check it out (unless of course you are already familiar with it).
Anyway, there's an additional feature called an ErrorProvider that you may want to use. Like Context Menus, you won't see any reference to this control until you drag one on your form. Once you do, each control that has a Validating event will have an Error On Provider property. The most logical place to respond is in the Validating event of your given control, but that's up to you.
Anyway, all you need to do is call the ErrorProvider, pass in a control to reference and then the message that you want it to display. There are also some other properties like BlinkRate, BlinkStyle and Icon to name a few that you may want to play with. Now, I'm mixed on this control b/c I prefer not to let the user do things as opposed to letting them mess up and barking at them with annoying dialog boxes or blinking things. However, this tool lets you give a relatively inobtrusive message that can contain useful information (the info only displays on MouseOver).
Well, here it is:
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles
TextBox1.Validating
'I'm checking that the Text.Length is Greater than 0, but any rule you want
'should be substituted
If Not TextBox1.Text.Length > 0 Then
ErrorProvider1.SetError(TextBox1, "Yuck")
'Insert Message here or Fire off an error Provider
e.Cancel = True
End If
End Sub |
|