Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WinForms ErrorProvider Control

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

like image 780
codingbadger Avatar asked Apr 21 '10 10:04

codingbadger


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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; } }
}
like image 116
Hans Passant Avatar answered Oct 04 '22 16:10

Hans Passant


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();
like image 29
lk74 Avatar answered Oct 04 '22 16:10

lk74