Click here to Skip to main content
15,175,198 members
Articles / Programming Languages / C#
Posted 10 Jan 2001


34 bookmarked

How To Use The ErrorProvider Object To Indicate Invalid Control State

Rate me:
Please Sign up or sign in to vote.
3.16/5 (15 votes)
1 Feb 2001
A tutorial on how to incorporate the ErrorProvider object into .NET WinForms using C#
  • Download source files - 9 Kb
  • Download demo project - 23 Kb

    Sample Image - ErrorProvider.jpg

    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");
    	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.

  • License

    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


    About the Author

    Naveen K Kohli
    United States United States
    No Biography provided

    Comments and Discussions

    QuestionToolTip Duration Pin
    mmcdonald2k8-Jun-12 8:47
    Membermmcdonald2k8-Jun-12 8:47 
    GeneralMy vote of 2 Pin
    DotNetCoderJunior3-Sep-10 3:47
    MemberDotNetCoderJunior3-Sep-10 3:47 
    QuestionHow many controls errors ? Pin
    Member 45801527-Oct-09 21:10
    MemberMember 45801527-Oct-09 21:10 
    GeneralTry again Pin
    Stevepsi20-Feb-08 8:20
    MemberStevepsi20-Feb-08 8:20 
    GeneralRe: Try again Pin
    Marc Leger21-Jan-09 6:59
    MemberMarc Leger21-Jan-09 6:59 
    GeneralRe: Try again Pin
    Darekk7-Mar-11 23:53
    MemberDarekk7-Mar-11 23:53 
    GeneralComm'on CodeMonkeys [modified] Pin
    cisjokey27-Nov-07 5:57
    Membercisjokey27-Nov-07 5:57 
    GeneralBe serious man! Pin
    carodiu2-Jul-07 8:53
    Membercarodiu2-Jul-07 8:53 
    Generaldoesn't work Pin
    Glen Harvy31-Jul-06 2:15
    MemberGlen Harvy31-Jul-06 2:15 
    Generaldoesnt work Pin
    MrPolite10-Feb-04 19:09
    MemberMrPolite10-Feb-04 19:09 
    GeneralThis sample does not compile. Pin
    TomR9-Apr-03 8:06
    MemberTomR9-Apr-03 8:06 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.