Is it possible to enumerate all the current errors being displayed through an "Error Provider" without having to access the controls?
4 回答
You can get all of the errors from an ErrorProvider by enumerating the Controls collection of its parent and calling GetError on each. Not efficient but it works.
foreach (Control ctrl in errProv.ContainerControl.Controls)
{
Console.WriteLine(errProv.GetError(ctrl));
}
For any .net WinForms people who find this in google etc...
In WinForms at least enumerating all the current errors being displayed through an "Error Provider" class without accessing all the controls is not possible, there isn't even a summary validator in WinForms.
However if your errors are bubbling up from a lower layer then you should have access to a collection of them somewhere anyway, as the poster Charles Graham points out.
In WinForms, if your application is simple enough not to have any well-defined "layers" then you could wrap the ErrorProvider in a class that records and exposes all current errors. Or, if the app is really really simple, create a helper method that records/deletes an error and updates the ErrorProvider.
There is a summary validator that will give you all of the errors, but it's pretty ugly, and I'm not sure if you can use it without displaying it on the page. Technically, if you are doing things the "right way", all of you error handling should be handled in your midddle teir and then bubbled to the screen that way, so you already have access to all the errors in a collection or dictionary.