views:

580

answers:

3

Hi

Does anyone know if there is a way to get a list of controls that have the ErrorProvider icon active. ie. any controls that failed validation. I'm trying to avoid looping all controls in the form.

I'd like to display some sort of message indicating how many errors there are on the form. As my form contains tabs I'm trying to make it apparent to the user that errors may exist on inactive tabs and they need to check all tabs.

Thanks

Barry

A: 

This is a moderately tricky solution you are talking about.

There is no way to achieve this automatically, as far as I know.

You have to maintain a flag for every control and manually set it every time an error-provider is blinked.

May be a Dictionary<TKey, TValue> can be used to keep track of it.

JMSA
A: 

You have to use SetError to set the error on the control in the first place, right? Perhaps you should store that information in another collection at the same time if you want to have it handy. For example, you could add each control with an error to a hashset.

BlueMonkMN
When using DataBinding the IDataErrorInfo-Interface is used and you have nothing to deal with it.
BeowulfOF
+1  A: 

This falls in the category of "how can you not know". It is your code that is calling ErrorProvider.SetError(), you should have no trouble keeping track of how many errors are still active. Here's a little helper class, use its SetError() method to update the ErrorProvider. Its Count property returns the number of active errors:

private class ErrorTracker {
  private HashSet<Control> mErrors = new HashSet<Control>();
  private ErrorProvider mProvider;

  public ErrorTracker(ErrorProvider provider) { 
    mProvider = provider; 
  }
  public void SetError(Control ctl, string text) {
    if (string.IsNullOrEmpty(text)) mErrors.Remove(ctl);
    else mErrors.Add(ctl);
    mProvider.SetError(ctl, text);
  }
  public int Count { get { return mErrors.Count; } }
}
Hans Passant
Thanks Hans. I has a feeling that I would have to do something like this. Depending on my time constraints, I might even create my own custom errorprovider for future use.It does seem like that this functionality should be available with the standard control - maybe that is just me.Thanks again.
Barry
Well, it is. You explicitly forbid using GetError() in your question. Be careful what you ask for.
Hans Passant