C# WinForms ErrorProvider 控件

发布于 2024-08-30 01:47:31 字数 174 浏览 8 评论 0原文

有谁知道是否有办法获取具有活动 ErrorProvider 图标的控件列表。 IE。任何未通过验证的控件。我试图避免循环表单中的所有控件。

我想显示某种消息,指示表单上有多少错误。由于我的表单包含选项卡,我试图让用户明白非活动选项卡上可能存在错误,他们需要检查所有选项卡。

谢谢巴里

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丑疤怪 2024-09-06 01:47:31

这属于“你怎么可能不知道”的范畴。您的代码正在调用 ErrorProvider.SetError(),您应该可以轻松跟踪有多少错误仍然处于活动状态。这是一个小帮助器类,使用它的 SetError() 方法来更新 ErrorProvider。其 Count 属性返回活动错误的数量:

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

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; } }
}
才能让你更想念 2024-09-06 01:47:31

今天我遇到了同样的问题。我的解决方案是扩展 ErrorProvider 控件。

请参阅下面的代码:

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

您可以在表单中使用上面的派生类,然后(假设 myErrorProvider 是表单中的类实例)您可以通过调用来获取表单中所有有错误的控件:

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

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();
御守 2024-09-06 01:47:31

这是您所说的一个比较棘手的解决方案。

据我所知,没有办法自动实现这一点。

您必须为每个控件维护一个标志,并在每次错误提供者闪烁时手动设置它。

可以使用 Dictionary 来跟踪它。

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.

折戟 2024-09-06 01:47:31

您首先必须使用 SetError 在控件上设置错误,对吗?如果您想方便地使用该信息,也许您应该同时将该信息存储在另一个集合中。例如,您可以将每个有错误的控件添加到哈希集中。

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.

旧话新听 2024-09-06 01:47:31

只需将 errorprovider 作为全局变量而不是局部变量

public partial class MainForm
 {

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


 }

即可

public partial class MainForm
 {

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


 }

解决您的问题,因为您可能已经从其他方法(例如 btnCancel_click)中删除了错误。
这对我有用:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文