C# WinForms ErrorProvider 控件
有谁知道是否有办法获取具有活动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这属于“你怎么可能不知道”的范畴。您的代码正在调用 ErrorProvider.SetError(),您应该可以轻松跟踪有多少错误仍然处于活动状态。这是一个小帮助器类,使用它的 SetError() 方法来更新 ErrorProvider。其 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:
今天我遇到了同样的问题。我的解决方案是扩展 ErrorProvider 控件。
请参阅下面的代码:
您可以在表单中使用上面的派生类,然后(假设 myErrorProvider 是表单中的类实例)您可以通过调用来获取表单中所有有错误的控件:
Today I had the same problem. My solution is to extend the ErrorProvider control.
See the code below:
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:
这是您所说的一个比较棘手的解决方案。
据我所知,没有办法自动实现这一点。
您必须为每个控件维护一个标志,并在每次错误提供者闪烁时手动设置它。
可以使用
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.您首先必须使用 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.
只需将 errorprovider 作为全局变量而不是局部变量
即可
解决您的问题,因为您可能已经从其他方法(例如 btnCancel_click)中删除了错误。
这对我有用:)
Just make the errorprovider as a Global variable rather than local variable
from
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 :)