C# WinForms ErrorProvider Control

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 if (!mErrors.Contains(ctl)) mErrors.Add(ctl);
    mProvider.SetError(ctl, text);
  }
  public int Count { get { return mErrors.Count; } }
}

Leave a Comment