The .Net framework provides several mechanisms for indicating invalid user inputs. You can use a MessageBox to indicate that the state of the control is not valid. A MessageBox can then provide the reason and remedies in the popup dialog box. Having several controls that needs user input this approach can be both cumbersome and intrusive for the user. Other approaches could be a balloon message, using the status panel or giving an audio clue of the invalid input. The .Net framework also provides a control named ErrorProvider, which is a control you can attach to any control that needs validation logic. If the error description string is set, an icon will appear next to the control. Hovering the mouse over the icon, a tooltip will display the error description string.
How to use the ErrorProvider
You can either drag the control from the ToolBox or you can programatically create an instance of the ErrorProvider. To modify the behavior, you can set the following properties:
- BlinkRate: This is the rate in milliseconds with which the icon will blink.
- BlinkStyle: This property can have one of the following values, AlwaysBlink, BlinkIfDifferentError or NeverBlink.
- Icon: The icon that is displayed when the error description string is set.
- IconAlignment: Where the icon should be placed relative to the control.
- IconPadding: The extra space between the error icon and the control.
Whenever the control’s state is invalid, you set the error description string, by using the SetError method on the ErrorProvider control.
This displays the error symbol next to the control.
Localization difficulties
This is a very useful control for providing non-intrusive feedback to the user. But, there is something that bothers every time I’m using this control. Why do you have to set a ‘string’ to display an icon?! I don’t get it. Its the only control in the .Net framework that uses this “mechanism”. Elsewhere its either a Start() method, Enable() method etc.
By combining a string with control display features it is difficult to use localization. In many applications it is evident to provide error descriptors in different languages, but when you cannot set the descriptor in whatever language without displaying the icon, you face additional work. Fortunately there are multiple options to come around this. One neat and clean solution is to create a user control that inherites the ErrorProvider. The new user control holds the local error descriptor string, which you can change whenever you’d like to without displaying the icon. Then you provide an Enable() method which calls SetError with the local string and displays the icon.