Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Extension for the ErrorProvider that Shows the Number of Errors

0.00/5 (No votes)
26 Oct 2020 1  
Simple class that uses composition to encapsulate the ErrorProvider and make available the number of errors
I found it very annoying that the error provider doesn't provide the number of errors. This control encapsulates the ErrorProvider to make the ErrorCount available.

Introduction

This control encapsulates the C# ErrorProvider and exposes an ErrorCount property which is missing from the standard control.

Using the Code

First, create an instance of the control in the load or shown event:

private void InterestForm_Shown(object sender, EventArgs e) {
    accepted = false;
    epMain = new ErrorProviderExt(this);
}

Then use it on the controls for validation:

private void txtLowValue_Validating(object sender, CancelEventArgs e) {
    if (isValidValue(cbType.Text, txtLowValue.Text) == false) {
        epMain.SetError(txtLowValue, "invalid value");
    } else {
        epMain.ClearErrors(txtLowValue);
    }
}

You can use the ErrorCount property to determine if the form contains any errors:

private void btnOk_Click(object sender, EventArgs e) {
    if (epMain.ErrorCount == 0) {
        accept = true;
        Close();
    }
}

The code for the control follows:

    public class ErrorProviderExt {
        private Dictionary<Control, string> _errorList = new Dictionary<Control, string>();
        private ErrorProvider provider = null;

        public ErrorProvider Provider { get { return provider; } }
        public int ErrorCount { get { return _errorList.Count; } }

        public ErrorProviderExt(Form f){
            provider = new ErrorProvider(f);
        }

        public void ClearErrors() {
            _errorList.Clear();
            provider.Clear();
        }

        public void ClearErrors(Control c) {
            if (_errorList.ContainsKey(c)) {
                _errorList.Remove(c);
                provider.SetError(c, null);
            }
        }

        public void SetError(Control c, string error) {
            if (error == null || String.IsNullOrWhiteSpace(error)) {
                ClearErrors(c);
            } else {
                if (_errorList.ContainsKey(c)) {
                    _errorList[c] = error;
                } else {
                    _errorList.Add(c, error);
                }
                provider.SetError(c, error);
            }
        }

        public bool HasError(Control c) {
            string error = null;
            if (_errorList.ContainsKey(c)) {
                error = _errorList[c];    
            }
            if (string.IsNullOrWhiteSpace(error)) {
                return false;
            } else {
                return true;
            }
        }               
    }
}

History

  • 26th October, 2020: Initial version

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