Download source files - 9 Kb
Download demo project - 23 Kb
In every GUI application, it is very likely that a user will enter some invalid
data or perform some action that will make the state of a control invalid. There
are couple of ways that an application can indicate to the user that state of a
control is not valid and that s/he needs to fix the problem before proceeding
further. The first approach is our good old MessageBox popup.
You can display the reason and remedies in the popup dialog box. The second
approach is using ErrorProvider object. You can attach this control
to any control that you want to add validation logic. ErrorProvider
presents a simple user interface for indicating to the user that a control on a
form has an error associated with it. If a error description string is specified
for the control, then an icon will appear next to the control. When the mouse
hovers over the icon, a tooltip will appear showing the error description string.
How To Use ErrorProvider
It is very simple to use the ErrorProvider object in your
application. Create an instance of this control object and then assign the
properties you would like this control to have. You can set the following
properties for ErrorProvider object.
BlinkRate
– This is rate in milliseconds at which the error icon flashes. This
property can have values like AlwaysBlink or BlinkIfDifferentError or NeverBlink.
Icon - The icon that is displayed next to a control when an error description
string has been set for the control.
IconAlignment - Where the error icon should be placed relative to the control.
IconPadding - Sets the amount of extra space to leave next to the error icon.
There are some more properties that can be manipulated, but the above-mentioned are
the most commonly used. The code below shows how this can be accomplished. For detailed
implementation, refer to the source file provided with this article.
protected ErrorProvider m_errProvider;
m_errProvider = new ErrorProvider ();
m_errProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;
m_errProvider.SetIconAlignment (this, ErrorIconAlignment.MiddleRight);
m_errProvider.SetIconPadding (this, 2);
Whenever you find that a certain control's state is invalid, use the SetError method
on the ErrorProvider object to show the icon next to that control.
if (!IsValidText (box.Text))</code>
{
m_errProvider.SetError (this, "Only integer values allowed");
}
else
{
m_errProvider.SetError (this, "");
}
If the error string is empty then the ErrorProvider icon will be hidden,
otherwise the icon will display at the location specified. The first parameter to the
SetError method is the control object to which you want to attach the
ErrorProvider icon. The second parameter is the tooltip text that will
be displayed when user moves the mouse over the flashing icon.
The demo project included with this article contains two edit boxes. One is configured for
entering double data and the second one is for entering integer data. Whenever you try to
enter an letter into these edit boxes, an icon will start flashing to the right of the edit
box. You can move the mouse over the flashing icon to see the message.
For more details on the implementation, refer to the source files included with article. And
for details on the ErrorProvider object, please refer to the .NET SDK online help.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here