ErrorProvider


The ErrorProvider component indicates to the user that another control has an error associated with it.

The following code shows how a program might use an ErrorProvider component. Suppose that the TextBox txtZip should contain a five-digit ZIP code. When the user enters a value and moves to another field, the control’s Validating event handler, shown in the code, fires. The code uses a Like statement to see if the text contains exactly five digits.

If the text does not contain five digits, then the program calls the ErrorProvider component’s SetError method to associate the error “Invalid ZIP code format” with the TextBox. At this point, the Error?Provider component displays an icon showing a little red circle containing a white exclamation mark next to the text box and makes the icon blink several times to draw the user’s attention to the error. If the user hovers the mouse over the error icon, the component displays a tooltip showing the error message.

If the text does contain five digits, then the code calls the ErrorProvider component’s SetError method to clear any error currently set for the txtZip TextBox. This makes the ErrorProvider remove any error icon it is currently displaying next to the control.

  ' Verify that this is a 5-digit ZIP code. Private Sub txtZip_Validating(ByVal sender As Object, _  ByVal e As System.ComponentModel.CancelEventArgs) Handles txtZip.Validating     If Not (txtZip.Text Like "##") Then         ' It's invalid. Display an error.         ErrorProvider1.SetError(txtZip, "Invalid ZIP code format")     Else         ' It's valid. Clear any error.         ErrorProvider1.SetError(txtZip, "")     End If End Sub 

The component’s BlinkRate property determines how quickly the error icon blinks. The BlinkStyle property determines how it blinks.

If BlinkStyle is BlinkIfDifferentError, then the component makes the icon blink several times whenever the program sets a control’s error message to a new nonblank value. Because the previous code uses the same error message whenever the ZIP code value does not contain exactly five digits, the message remains unchanged if the user changes the ZIP code to a new invalid value, so the icon does not blink again.

If BlinkStyle is AlwaysBlink, then the component makes the icon blink as long as the control has an associated error. The icon continues blinking, even if another application has the focus, until the user fixes the error and moves to a new field to trigger the Validating event handler again.

If BlinkStyle is NeverBlink, then the component displays the error icon without blinking.

The component’s Icon property gives the icon that the component displays for an error. You can change this icon if you want to use a special error image.

Tip 

Blinking text can be extremely irritating to users, so don’t abuse it. The default behavior of blinking a couple of times and then stopping is probably reasonable, but a message that is constantly blinking very quickly is a bad idea. Note that the United States Accessibility code prohibits objects that blink faster than 2 Hz and slower than 55 Hz (see 508.nih.gov/saos/excerpted_stds.html) at least in part because blinking can cause seizures and other life-threatening reactions in some people.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net