Check if an error is present with error provider

Vaibhav Jain picture Vaibhav Jain · Apr 4, 2012 · Viewed 10.4k times · Source

I am using an error provider on my windows form. On submit of the form, I need to check if there is some error present on the form. Can I get if there is a error present in the error provider.

Answer

surfen picture surfen · Apr 4, 2012

Write a Validate() method that returns false if it fails validation and execute it from SaveButton handler. You should perform re-validation of all controls of the form in this method. Continue Save only when Validate() returns true.

You could use ErrorProvider.GetError to get an error of a particular control on your form, but to get all errors on the entire form you would need to iterate over all controls that use that particular ErrorProvider instance on your form anyway.