Click here to Skip to main content
12,450,101 members (53,718 online)
Click here to Skip to main content
Add your own
alternative version

Stats

155.9K views
3.4K downloads
34 bookmarked
Posted

How To Use The ErrorProvider Object To Indicate Invalid Control State

, 1 Feb 2001
Rate this:
Please Sign up or sign in to vote.
A tutorial on how to incorporate the ErrorProvider object into .NET WinForms using C#
  • Download source files - 9 Kb <!-- Link to demo file download -->
  • Download demo project - 23 Kb <!-- Article image -->

    Sample Image - ErrorProvider.jpg

    <!-- Add the rest of your HTML here -->

    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>

    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

    Share

    About the Author

    Naveen K Kohli
    United States United States
    No Biography provided

    You may also be interested in...

    Pro
    Pro

    Comments and Discussions

     
    QuestionToolTip Duration Pin
    mmcdonald2k8-Jun-12 7:47
    membermmcdonald2k8-Jun-12 7:47 
    GeneralMy vote of 2 Pin
    DotNetCoderJunior3-Sep-10 2:47
    memberDotNetCoderJunior3-Sep-10 2:47 
    QuestionHow many controls errors ? Pin
    Member 45801527-Oct-09 20:10
    memberMember 45801527-Oct-09 20:10 
    GeneralTry again Pin
    Stevepsi20-Feb-08 7:20
    memberStevepsi20-Feb-08 7:20 
    GeneralRe: Try again Pin
    Marc Leger21-Jan-09 5:59
    memberMarc Leger21-Jan-09 5:59 
    GeneralRe: Try again Pin
    Darekk7-Mar-11 22:53
    memberDarekk7-Mar-11 22:53 
    GeneralComm'on CodeMonkeys [modified] Pin
    cisjokey27-Nov-07 4:57
    membercisjokey27-Nov-07 4:57 
    GeneralBe serious man! Pin
    carodiu2-Jul-07 7:53
    membercarodiu2-Jul-07 7:53 
    Generaldoesn't work Pin
    Glen Harvy31-Jul-06 1:15
    memberGlen Harvy31-Jul-06 1:15 
    Generaldoesnt work Pin
    MrPolite (Kourosh Derakshan)10-Feb-04 18:09
    memberMrPolite (Kourosh Derakshan)10-Feb-04 18:09 
    GeneralThis sample does not compile. Pin
    TomR9-Apr-03 7:06
    memberTomR9-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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.160826.1 | Last Updated 2 Feb 2001
    Article Copyright 2001 by Naveen K Kohli
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid