Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#
Article

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 176.2K   3.5K   35   11
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");
    } 
    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.

  • 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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionToolTip Duration Pin
    mmcdonald2k8-Jun-12 7:47
    mmcdonald2k8-Jun-12 7:47 
    GeneralMy vote of 2 Pin
    DotNetCoderJunior3-Sep-10 2:47
    DotNetCoderJunior3-Sep-10 2:47 
    QuestionHow many controls errors ? Pin
    Member 45801527-Oct-09 20:10
    Member 45801527-Oct-09 20:10 
    GeneralTry again Pin
    Stevepsi20-Feb-08 7:20
    Stevepsi20-Feb-08 7:20 
    GeneralRe: Try again Pin
    Marc Leger21-Jan-09 5:59
    Marc Leger21-Jan-09 5:59 
    You're commenting on a post made 8 years ago? Confused | :confused:
    GeneralRe: Try again Pin
    Darekk7-Mar-11 22:53
    Darekk7-Mar-11 22:53 
    GeneralComm'on CodeMonkeys [modified] Pin
    cisjokey27-Nov-07 4:57
    cisjokey27-Nov-07 4:57 
    GeneralBe serious man! Pin
    carodiu2-Jul-07 7:53
    carodiu2-Jul-07 7:53 
    Generaldoesn't work Pin
    Glen Harvy31-Jul-06 1:15
    Glen Harvy31-Jul-06 1:15 
    Generaldoesnt work Pin
    MrPolite10-Feb-04 18:09
    MrPolite10-Feb-04 18:09 
    GeneralThis sample does not compile. Pin
    TomR9-Apr-03 7:06
    TomR9-Apr-03 7: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.