Results 1 to 2 of 2

Thread: Confused about proper use of ErrorProviders

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Kansas, USA

    Confused about proper use of ErrorProviders

    I have 5 controls that I need to check for errors. I have set up all the events on text changed event and identified the errors properly. I was pointed to ErrorProviders as a way to display that the form text box (or other control) has an error. It works great for that. Now my issues are understanding the best strategy for using ErrorProviders and how to enable/disable a save button when all the errors are clear.

    I have an ErrorProvider for each control. Some controls are simple and just need to have any value, other have lengths (min and max) and exclusion of illegal characters.

    What is best practices for ErrorProviders? I see that all five of my controls now show all five of the ErrorProviders in their properties which makes me think that perhaps rather than have an ErrorProvider for each control that I need an ErrorProvider for each TYPE of error:

    Cannot be blank
    Cannot be less that 6 characters
    Cannot be more than 255 characters
    Cannot have illegal characters

    Also, how should ErrorProviders be checked for errors to enable or disable the save button? My thought is to use a method that checks all the error controls every time any of the events that check for errors are fired.
    -------------------------------------------------------------------------------------------------------------------- ... Visual Studio 2019
    "None of us are as smart as all of us."

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Confused about proper use of ErrorProviders

    You only need one ErrorProvider per form. Just like a ToolTip, it can display an error icon for as many controls as you like. You'd only need more than if you want to use different icons at the same time.

    Generally, speaking, you should handle the Validating event of each of your controls and then set or clear the error there. You can do it on other events if you want though. You simply call SetError on the one and only ErrorProvider and pass the appropriate control and the error message. To clear an error, just pass 'null' as the error message.

    The ErrorProvider has a GetError method that will return the current error message for a control. You can call that for each control and then enable or disable your Button based on whether any are not empty. The following code will work as long as every control is directly on the form, i.e. not in a Panel or other child container:
    csharp Code:
    1. button1.Enabled = Controls.Cast<Control>()
    2.                           .Select(c => errorProvider1.GetError(c))
    3.                           .All(s => string.IsNullOrEmpty(s));
    That will get the error message for every control on the form and return 'true' if an only if they are all 'null' or an empty String.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width