Click here to Skip to main content
Licence 
First Posted 10 Jan 2001
Views 117,100
Bookmarked 31 times

How To Use The ErrorProvider Object To Indicate Invalid Control State

By | 1 Feb 2001 | Article
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

    About the Author

    Naveen K Kohli



    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralMy vote of 2 PinmemberDotNetCoderJunior2:47 3 Sep '10  
    QuestionHow many controls errors ? PinmemberMember 45801520:10 27 Oct '09  
    GeneralTry again PinmemberStevepsi7:20 20 Feb '08  
    GeneralRe: Try again PinmemberMarc Leger5:59 21 Jan '09  
    GeneralRe: Try again PinmemberDarekk22:53 7 Mar '11  
    GeneralComm'on CodeMonkeys [modified] Pinmembercisjokey4:57 27 Nov '07  
    GeneralBe serious man! Pinmembercarodiu7:53 2 Jul '07  
    Generaldoesn't work PinmemberGlen Harvy1:15 31 Jul '06  
    Generaldoesnt work PinmemberMrPolite (Kourosh Derakshan)18:09 10 Feb '04  
    GeneralThis sample does not compile. PinmemberTomR7:06 9 Apr '03  

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

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

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