C# WinForms ErrorProvider Control

…衆ロ難τιáo~ 提交于 2019-11-26 20:49:42

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; } }
}

Today I had the same problem. My solution is to extend the ErrorProvider control.

See the code below:

  public class MyErrorProvider : ErrorProvider
  {

    public List<Control> GetControls()
    {
      return this.GetControls(this.ContainerControl);
    }

    public List<Control> GetControls(Control ParentControl)
    {
      List<Control> ret = new List<Control>();

      if (!string.IsNullOrEmpty(this.GetError(ParentControl)))
        ret.Add(ParentControl);

      foreach (Control c in ParentControl.Controls)
      {
        List<Control> child = GetControls(c);
        if (child.Count > 0)
          ret.AddRange(child);
      }

      return ret;
    }
  }

You can use the above derived class in your form, and then (say that myErrorProvider is the class instance in your form) you can get all the controls with errors in your form, by calling:

List<Control> errorControls = myErrorProvider.GetControls();

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.

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.

Just make the errorprovider as a Global variable rather than local variable

public partial class MainForm
 {

    ErrorProvider errorProvider1 = new ErrorProvider();
    void Validate_Working()
    {
    errorProvider1.SetError(textbox1, "textbox is empty");
    errorProvider1.Clear();
    }


 }

from

public partial class MainForm
 {

    Void Validate_NotWorking()
    {
    ErrorProvider errorProvider1 = new ErrorProvider();
    errorProvider1.SetError(textbox1, "textbox is empty");
    errorProvider1.Clear();
    }


 }

This should fix your problem, because probably you might have been removing your errors from another method such as btnCancel_click. This worked for me :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!